2014-01-09 12 views
0

Моя Manifest активность кодаAndroid путь к файлу изображения из URI

 <activity android:name="FileReceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.SEND" /> 

      <category android:name="android.intent.category.DEFAULT" /> 

      <data android:mimeType="image/*" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.SEND_MULTIPLE" /> 

      <category android:name="android.intent.category.DEFAULT" /> 

      <data android:mimeType="image/*" /> 
     </intent-filter> 
    </activity> 

Мой код активность:

Intent i = getIntent(); 
    String action = i.getAction(); 


if (action.equals(Intent.ACTION_SEND)) { 
     Uri imageUri = (Uri) i.getParcelableExtra(Intent.EXTRA_STREAM); 
     if (imageUri != null) { 
      Log.i("1234567", "URI : " + imageUri); 
      Log.i("123456", "path :" + imageUri.getPath()); 
      File source = null; 
      source = new File(imageUri.getPath()); 
      String fileNme = "/" + source.getName(); 

      File destination = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path); 
      Log.i("123456", "destination path :" + destination); 
      try { 
       FileUtils.copyFile(source, destination); 
      } catch (IOException e) { 
       Log.i("123456", "IO Exception"); 
       e.printStackTrace(); 
      } 

     } 

им получить исключение

01-09 16:10:09.756: W/System.err(30225): java.io.FileNotFoundException: Source 'content:/media/external/images/media/127' does not exist 

как я получаю путь к изображению когда я получаю изображение из папки DCIM?

+0

Есть ли в вашем файле манифеста разрешение на чтение и запись? – Piyush

+0

Да, я добавил. – Meher

ответ

1
/** 
* helper to retrieve the path of an image URI 
*/ 
public String getPath(Uri uri) { 
     // just some safety built in 
     if(uri == null) { 
      // TODO perform some logging or show user feedback 
      return null; 
     } 
     // try to retrieve the image from the media store first 
     // this will only work for images selected from gallery 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if(cursor != null){ 
      int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
     // this is our fallback here 
     return uri.getPath(); 
} 

Источник: Get/pick an image from Android's built-in Gallery app programmatically

0

У меня такая же проблема. Выбрал его, используя следующий код:

public static String getPathFromURI(Context context, Uri contentUri) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && 
      DocumentsContract.isDocumentUri(context, contentUri)) { 
     return getPathForV19AndUp(context, contentUri); 
    } else { 
     return getPathForPreV19(context, contentUri); 
    } 
} 

private static String getPathForPreV19(Context context, Uri contentUri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null); 
    if (cursor != null && cursor.moveToFirst()) { 
     try { 
      int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      return cursor.getString(columnIndex); 
     } finally { 
      cursor.close(); 
     } 
    } 

    return null; 
} 

@TargetApi(Build.VERSION_CODES.KITKAT) 
private static String getPathForV19AndUp(Context context, Uri contentUri) { 
    String documentId = DocumentsContract.getDocumentId(contentUri); 
    String id = documentId.split(":")[1]; 

    String[] column = { MediaStore.Images.Media.DATA }; 
    String sel = MediaStore.Images.Media._ID + "=?"; 
    Cursor cursor = context.getContentResolver(). 
      query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        column, sel, new String[]{ id }, null); 

    if (cursor != null) { 
     try { 
      int columnIndex = cursor.getColumnIndex(column[0]); 
      if (cursor.moveToFirst()) { 
       return cursor.getString(columnIndex); 
      } 
     } finally { 
      cursor.close(); 
     } 
    } 

    return null; 
} 
Смежные вопросы