PDA

View Full Version : خطا در گرفتن آدرس فایل



Mansoor92
شنبه 03 مرداد 1394, 09:44 صبح
سلام

من با استفاده از دستورات زیر یک فایل دیالوگ باز میکنم و یک ایمیج انتخاب میکنم ولی وقتی میخوام آدرسش رو بگیرم چنین آدرسی به من میده

File Uri: content://com.android.providers.media.documents/document/image%3A70


کد هام هم اینا هست


private static final int FILE_SELECT_CODE = 0;

private void showFileChooser() {
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();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.d("Path", "File Uri: " + uri.toString());
// Get the path
String path = null;
try {
path = getPath(this, uri);
} catch (URISyntaxException e) {
e.printStackTrace();
}
Log.d("Path", "File Path: " + path);
// Get the file instance
// File file = new File(path);
// Initiate the upload
addres=path;
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

public static String getPath(Context context, Uri uri) throws URISyntaxException {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;

try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}

return null;
}

Mansoor92
یک شنبه 04 مرداد 1394, 15:48 عصر
دوستان لطفا راهنمایی کنید.