How To Remove Filenotfoundexception: Exception In Android?
Solution 1:
What you get is not a file path for the file system but a media path in use by mediastore and others. In order to get a real filepath you have to invoke a cursor on the media store. You can find many example codes on this site.
Solution 2:
Your real issue is to convert the image into byte so you can Upload on to a server. The onActivityResult method will provide you with a Bitmap object.
protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
}
}
Now instead of getting the Path, then converting it into File and then again converting it into bytes, you can directly use the bitmap object that you get from onActivityResult method and use the below method to convert it into bytes.
publicbyte[] bitmapToByte(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
But this post is asking "How to remove FileNotFoundException: exception in android?", so to get the path of the image you need to:
protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmapphoto= (Bitmap) data.getExtras().get("data");
// CALL THIS METHOD TO GET THE URI FROM THE BITMAPUritempUri= getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATHFilefinalFile=newFile(getAbsolutePathFromURI(tempUri));
}
}
public Uri getImageUri(Context context, Bitmap bitmap) {
ByteArrayOutputStreambytes=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Stringpath= Images.Media.insertImage(context.getContentResolver(), bitmap, "TempImage", null);
return Uri.parse(path);
}
public String getAbsolutePathFromURI(Uri uri) {
Cursorcursor= getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
intindex= cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(index);
}
Solution 3:
There are permissions that you need to add to your android manifest although this may not be your only problem. Add:
<manifest...><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
Post a Comment for "How To Remove Filenotfoundexception: Exception In Android?"