2016-11-11 73 views
-1

問題是我正在嘗試從圖庫中選擇圖像。之後,如果我從圖庫中打開原始圖像,它不會打開(黑色屏幕即將到來)!即使我試圖從應用程序中再次選擇該圖片,它也會給出像這樣的錯誤無法加載圖片從圖庫中選擇圖片android原始圖像未從外部打開

private void openGalleryForImageSelection() 
{ 
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    try 
    { 
     startActivityForResult(intent, IMAGE_FROM_GALLERY); 
    } 
    catch(Throwable e) 
    { 
     Log.e(LOG_TAG,"openGalleryForImageSelection failed",e); 
     Toast.makeText(this,getResources().getString(R.string.image_error),Toast.LENGTH_SHORT).show(); 
    } 
} 

請幫我解決這個問題。謝謝。

+0

你是否在'onActivityResult()'方法內正確獲取圖像路徑? –

+0

謝謝你的迴應。我正確地獲取圖像路徑。任何圖像第一次開放。但如果我再次嘗試再次選擇相同的圖片,它不會打開!即使我試圖從外面打開相同的圖像,它會給出一個空白的圖像! –

回答

0

首先將這些權限添加到您的menifest中。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

現在在任何地方調用showFileChooser()函數。

private void showFileChooser() { 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.setType("image/*"); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     try { 
      startActivityForResult(
        Intent.createChooser(intent, "Select a Image to Upload"), 
        1); 
     } catch (android.content.ActivityNotFoundException ex) { 
      Toast.makeText(getApplicationContext(), "Please install a Photo Viewer.", 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 
@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == 1) { 
      if (resultCode == Activity.RESULT_OK) { 

      } 
     } 
} 
0

openGalleryForImageSelection()方法更改爲:

public void openGalleryForImageSelection(View view) { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_FROM_GALLERY); 
} 

然後你會進去onActivityResult()意圖的數據,然後通過調用getPathForImage()這樣得到它的真實路徑:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == 1) { 
     if (resultCode == Activity.RESULT_OK) { 
     realPath = getPathForImage(ProfileActivity.this, data.getData()); 
     } 
    } 
} 


public static String getPathForImage(Context context, Uri uri) 
{ 
    String result = null; 
    Cursor cursor = null; 

    try { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     cursor = context.getContentResolver().query(uri, proj, null, null, null); 
     if (cursor == null) { 
      result = uri.getPath(); 
     } else { 
      cursor.moveToFirst(); 
      int column_index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
      result = cursor.getString(column_index); 
      cursor.close(); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
    return result; 
} 

並使用realPath你得到了onActivityResult轉換成Bitmap並加載那個Bitmap進入你的ImageView 這是做到這一點的方法。希望它能幫助你

+0

嘗試了相同的代碼,但getPathForImage方法返回空值! –

+0

當它打開圖像時,不要從'Recent'中選擇圖像,嘗試從'Gallery'文件夾中選擇它,並讓我知道它是否工作。 –