2017-02-16 117 views
0

(我很抱歉,如果我用英文書寫錯誤) 我需要按照標題說 我只知道如何從畫廊獲得圖像。我認爲這是正確的:如何從圖庫中獲取圖像,獲取圖像名稱並將其保存在內部存儲器中並獲取該圖像的路徑

(如果是錯誤的,或者它是無效的,請讓我知道)

private final int SELECT_PICTURE = 200; 

btnAddImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent galeryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      galeryIntent.setType("image/*"); 
      startActivityForResult(galeryIntent.createChooser(galeryIntent, "Sececiona Imagen"), SELECT_PICTURE); 
     } 
    }); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode){ 
     case (SELECT_PICTURE): 
      if(requestCode == RESULT_OK){ 
       Uri path = data.getData(); 
       } 
      } 
      break; 
    } 
} 

但我不知道怎麼弄,我從捕捉圖像的名稱並在我的應用程序的內部存儲器中保存該名稱。 然後我需要像「MyAPP/media/...」這樣的圖像路徑,我不知道英文的正確名稱,並將其保存在我的數據庫中(保存在我的數據庫中很容易),然後在我的項目中使用它

我知道這是一個很長的消息,但我需要你的幫助,因爲在StackOverflow的西班牙論壇有人回答我。

回答

0

onActivityResult:

String path = getPath(getApplicationContext(), data.getData());  
File mFile = new File(id); 
mFile.getName(); 

這是一個得到了文件名的方式。

而且的getPath:

public static String getPath(final Context context, final Uri uri) { 

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 

    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
     if (isExternalStorageDocument(uri)) { 
      final String docId = DocumentsContract.getDocumentId(uri); 
      final String[] split = docId.split(":"); 
      final String type = split[0]; 

      Log.d("Lucas","TYPE => "+type); 

      if ("primary".equalsIgnoreCase(type)) { 
       return Environment.getExternalStorageDirectory() + "/" + split[1]; 
      } 
     } 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); 
     } 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); 
     } 
    } else if ("content".equalsIgnoreCase(uri.getScheme())) { 
     return getDataColumn(context, uri, null, null); 
    } else if ("file".equalsIgnoreCase(uri.getScheme())) { 
     return uri.getPath(); 
    } 

    return null; 
} 

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; 
} 

public static boolean isExternalStorageDocument(Uri uri) { 
    return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
} 

public static boolean isDownloadsDocument(Uri uri) { 
    return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
} 

public static boolean isMediaDocument(Uri uri) { 
    return "com.android.providers.media.documents".equals(uri.getAuthority()); 
} 
+0

@Isca Android的說:無法解析方法的getPath() – ProRiderZ115

+0

@ ProRiderZ115對不起,這裏是的getPath方法:) – lsca

+0

@Isca無法解析建立和isDocumentUri和getDocumentID中下劃線紅色 (很多thx爲你的幫助) – ProRiderZ115

相關問題