Metalhead1247 Metalhead1247 - 1 month ago 7
Java Question

Setting image using Imagepath does not work

Trying to set images to image view using the image paths.

My image path

String img_path ="/storage/sdcard0/Fbt/xylo.jpg";


image path code

private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
return cursor.getString(idx);
}
}

selectedImage = imageReturnedIntent.getData();
File imageFile = new File(getRealPathFromURI(selectedImage));
String path= imageFile.toString();


output =/storage/sdcard0/Fbt/xylo.jpg

I tried all possible code still there is no success


  1. ImageView carpict =(ImageView)findViewById(R.id.img);


    Bitmap bitmap = BitmapFactory.decodeFile(img_path);
    carpict.setImageBitmap(bitmap);



2.

Uri image22 =(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+img_path));


InputStream imageStream = null;

try {
imageStream = getContentResolver().openInputStream(image22);

Bitmap carpic = BitmapFactory.decodeStream(imageStream);


// carpic = Bitmap.createScaledBitmap(carpic, 72,72, false);
carpict.setImageBitmap(carpic);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


3rd.

carpict.setImageURI
(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+img_path));


4th.

File imgFile =new File("img_path");
carpict.setImageBitmap(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));


I have also added

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


can anyone tell me where i am going wrong?

Answer

The problem was with the image itself when i tried with other images it worked images with formats .jpg .png .jpeg

     ImageView carpict =(ImageView)findViewById(R.id.img);
     Bitmap bitmap = BitmapFactory.decodeFile(img_path);
     carpict.setImageBitmap(bitmap);

Here is the image xylo.jpg which is not getting set, don't know why

http://www.mediafire.com/?lh75aco1en8f3ot