2017-05-11 14 views
0

私は、shareコマンドでGalleryから画像を取得したいと思います。ギャラリーからインテントを取得する

私の現在のコードは次のとおりです。

Intent intent = getIntent(); 
    String action = intent.getAction(); 
    String type = intent.getType(); 

    if (Intent.ACTION_SEND.equals(action) && type != null) { 
     Log.d("Test","Simple SEND"); 
     Uri imageUri = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM); 
     if (imageUri != null) { 
      InputStream iStream = getContentResolver().openInputStream(imageUri); 
     } 
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { 
     Log.d("Test", "Multiple SEND"); 
    } 

imageUriの値は次のとおりです。内容://メディア/外部/画像/メディア/ 37

しかし、機能は "openInputStreamは" "エラーがスローされますjava.io.FileNotFoundException "を返します。

次の関数を使って、画像の実際のパスを取得します。

public static String getRealPathFromUri(Context context, Uri contentUri) { 
    Cursor cursor = null; 
    try { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
} 

しかし、ビットマップに変換する方法がわかりません。

答えて

0

Uriは、Fileではない。 new File(imageUri.toString())は常に間違っています。 imageUri.getPath()は、Uriのスキームがfileの場合にのみ機能し、その場合のスキームはおそらくcontentです。

ContentResolverおよびopenInputStream()を使用して、Uriで識別されるファイルまたはコンテンツにInputStreamを取得します。その後、そのストリームをBitmapFactory.decodeStream()に渡します。

また、バックグラウンドスレッドのビットマップをデコードして、この作業が進行中にUIをフリーズしないようにしてください。

+0

ご回答ありがとうございます。 私はこの行を追加しました: "InputStream iStream = getContentResolver()。openInputStream(imageUri);" しかし、その行は常に "java.io.FileNotFoundException"というエラーをスローします。 – WhatEver2337

+0

@ WhatEver2337: 'imageUri'の値は? – CommonsWare

+0

値は:content:// media/external/images/media/37 – WhatEver2337

関連する問題