2017-04-14 15 views
0

アルバムから画像を取得したいのですが、画像を選択すると表示されず停止します。私は自分の電話でテストしますが、問題は多分私のコードで間違った経路を設定していると思います。しかし、私はそれがどこにあるのか分かりません。 ケース100:ピクチャアクションを実行して機能し、ケース101:ピクチャアクションを選択します。 パスから画像を取得

この

は、これは私のconverUriである私のonActivityResult

protected void **onActivityResult**(int requestCode , int resultCode , Intent 
data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == Activity.RESULT_OK) { 
      switch (requestCode) 
      { 
       case 100: 
        Intent it = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
        sendBroadcast(it); 
        break; 
       case 101: 
        imgUri = convertUri(data.getData()); 

        break; 
      } 
     showImg(); 
    } 
    else { 
     Toast.makeText(this , requestCode==100? "no take the pic" : "no choose the pic" , Toast.LENGTH_LONG).show(); 
    } 
} 

ある

Uri convertUri(Uri uri){ 
    if(uri.toString().substring(0,7).equals("content")) 
    { 
     String[] colName = {MediaStore.MediaColumns.DATA}; 
     Cursor cursor = getContentResolver().query(uri,colName,null,null,null); 
     cursor.moveToFirst(); 
     uri = Uri.parse("file://"+ cursor.getString(0)); 
    } 
    return uri; 
} 

これは私のshowImag

void showImg(){ 
    imv = (ImageView) findViewById(R.id.imageView); 
    int iw ,ih , vw, vh; 
    boolean needRotate; 
    BitmapFactory.Options option = new BitmapFactory.Options(); 
    option.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(imgUri.getPath(),option); 

    iw = option.outWidth; 
    ih = option.outHeight; 
    vw = imv.getWidth(); 
    vh = imv.getHeight(); 

    int scaleFactor; 
    if(iw<ih) 
    { 
     needRotate = false; 
     scaleFactor = Math.min(iw/vw , ih/vh); 
    }else{ 
     needRotate = true ; 
     scaleFactor =Math.min(ih/vw , iw/vh); 
    } 
    option.inJustDecodeBounds = false; 
    option.inSampleSize = scaleFactor ; 
    Bitmap bmp = BitmapFactory.decodeFile(imgUri.getPath(),option); 
    if(needRotate) 
    { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(90); 
     bmp = Bitmap.createBitmap(bmp , 0 , 0 ,bmp.getWidth(), bmp.getHeight(), matrix , true); 
    } 
    imv.setImageBitmap(bmp); 
} 

答えて

0

であるあなたは、ファイルシステムを取得しようとしていると間違った方法でありますパス。

onActivityResultのdata.getData()uriを実際に使用する方がいいです。

Bitmap bmp = BitmapFactory.decodeFile(imgUri.getPath(),option); 

に変更:

InputStream is = getContentResolver().openInputStream(data.getData()); 
Bitmap bmp = BitmapFactory.decodeStream(is, option); 

最初の呼び出しのために同じことを行います。 isは1回だけ使用できます。

+0

あなたのansのためのthks、しかしそれは働くことができません。私が選ぶとき、それは止まった。私のconvertUriまたは私の電話または使用許可に何か問題がありますか? –

関連する問題