dimanche 22 février 2015

Android: Can't load images previously taken with the camera



I can load stock images that came with the phone in the gallery. But when I try loading an image from the camera directory (which is inside the same gallery), it doesn't show up.


But what's really weird is that it takes up the space as if the image was successfully loaded. So it takes up space, but it's invisible. (Confirmed this by giving the ImageView a black background that only appeared after loading an image from the camera directory.)


This is the permission I added to AndroidManifest.xml:



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


Here is the code used to load the image:



public void loadImagefromGallery(View view)
{
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try
{
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data)
{
// Get the Image from data

Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgVieww);
Bitmap bitmapForImage = BitmapFactory.decodeFile(imgDecodableString);

// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(bitmapForImage);
}
else
{
Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}

}


This was taken from a tutorial. If you'd like to try it yourself, it's only 1 class and 1 xml file: http://ift.tt/1D0jUeL




Aucun commentaire:

Enregistrer un commentaire