Hi Guys i am quite new in android.I am trying to convert file to byte array but it throw file not found error in this line
fis = new FileInputStream(new File(uri.getPath()));
Got stuck hear can any one have any idea This is My Intent Chooser
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
this is my onactivityresultcode.
Uri uri = data.getData();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(uri.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bbytes = baos.toByteArray();
I had also write code for display name.
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor =
ChatActivity.this.getContentResolver().query(uri, null, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
displayName =
cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
Log.d("displayname",displayName);
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
displayName = myFile.getName();
Log.d("filename",displayName);
}