بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
مشکل در دریافت مسیر فایل
سلام من میخواستم مسیر یک فایل و از طریق فایل بروزر بگیرم با این کد بازش کردم
Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
fileintent.setType("*/*");
fileintent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(fileintent,"Selected"),1);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
و توی ActivityResult با این کد یک چیزایی تونستم بگیرم
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
File file = new File(uri.getPath());
String path = file.getAbsolutePath();
txt.setText(path);
}
}
تقریبا همه توی نت همین و گفته بودن اما خب چیزی که برمیگردونه اینجوریه document/video:23 یا توی همین مایه ها. بعضی از فایل ها درست کار میکنه اما اکثرش نه .
0
0
لینک (10 سال پیش)
0
0
کار نمیکنه (10 سال پیش)
0
0
من دقیقا همون کد کپی کردم نشد نال برگردوند درصورتی که حداقل
data.getData().getPath()
ی چیزی بر میگردونه . مشکل از کجاست؟ (10 سال پیش)
0
0
پرمیژن کارت حافظه رو نذاشتید ! (10 سال پیش)
0
0
اشتباه شد ف به پرمیژن کارت کافظه داخلی ربطی نداره ولی ممکنه پرمیژن کارت حافظه خارجی نیاز باشه!! باید خط به خط کدتون رو بررسی کنید < این موضوع مسئله پیچیده ایی نداره ، معمولا خطای نوشتاری و نکته هایی جزئی باعث میشه نتیجه نگیرید (10 سال پیش)
0
0
خطی نداره دارم توی یک پروژه خالی تست میکنم کلا همه خطش همیناست . خیلی عجیبه :| روی بعضی از فایل منیجر ها درست کار میکنه (10 سال پیش)
+1
0
مشکل رفع شد آموزشش و میزارم بعدا . (10 سال پیش)
برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال
IIVII
10 سال پیش
+1
0
توی کد بالا اگر مقداری ارسال کنید احتمال اینکه اینجوری بهتون مسیر بده زیاده " document/vide:22 " کد پایین به شما کمک میکنه که مسیر این فایل و بخونه و بهتون مسیر اصلی بده یعنی اگر فایل شما در " sd/video/m.mkv " هست و به شما بالاییو میده این کد بدردتون میخوره
public class FileHandlerPath {
private static String getPathDeprecated(Context ctx, Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = ctx.getContentResolver().query(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
public static String getSmartFilePath(Context ctx, Uri uri){
if (Build.VERSION.SDK_INT < 19) {
return getPathDeprecated(ctx, uri);
}
return FileHandlerPath.getPath(ctx, uri);
}
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .