2017-03-01 5 views
0

res/drawableから取得したビットマップと比較して、intentから取得した同じビットマップの品質ははるかに低くなります。Android - ビットマップ解像度の損失

現在のプロジェクトでは画質が本当に心配ですから、元のビットマップ(圧縮や解像度の損失なし)を電話機のストレージから取得したいと考えています。

intentから高品質のビットマップを取得するにはどうすればよいですか?私はこのように、外部記憶装置からビットマップを取得するためにintentを使用

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    Button galleryBtn = new Button(getActivity()); 
    galleryBtn.setText("Import Photo..."); 
    galleryBtn.setOnClickListener(v -> { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     intent.setType("image/*"); 
     this.startActivityForResult(intent, 0); 
    }); 

    //... some more code here 
} 

//... some more code here 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 0) 
     try { 
      if (data != null) { 
       InputStream stream = getActivity().getContentResolver().openInputStream(
        data.getData()); 
       imageBitmap = BitmapFactory.decodeStream(stream); 
       stream.close(); 

       imageView.setImageBitmap(imageBitmap); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
} 

そして、私はこのようにresからビットマップを取得し、

imageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); 
+0

のgetDataは、サムネイルを返し、onActivityResultあなたにこれを試して 'getDataString()'その後、ウリ – tyczj

+0

@tyczj経由imaveを得る:ハァッか。 'data.getData()'は 'ACTION_GET_CONTENT'で選択されたコンテンツに' Uri'を返します。 – CommonsWare

+0

"同じビットマップのインテントからの品質がずっと低くなっています" - AFAIK、両方のコードパスが同じ画像デコードロジックを通過しています。あなたの質問を編集し、この効果を与えるビットマップにリンクし、リソースの使用結果を示すスクリーンショットへのリンクと 'ACTION_GET_CONTENT'が提供する' Uri'を使用してください。 – CommonsWare

答えて

-1

意図は合格するサイズ制限がありますインテントから、公共の静的オブジェクトを使用してサービスからブロードキャストにビットマップを渡すことができます

public class ImageBox { 
    public static Queue<Bitmap> mQ = new LinkedBlockingQueue<Bitmap>(); 
} 
それはあなたのために働く必要があるサービスでの

パス、

private void downloadFile(final String url){ 
     mExecutorService.submit(new Runnable() { 
      @Override 
      public void run() { 
       Bitmap b = BitmapFromURL.getBitmapFromURL(url); 
       synchronized (this){ 
        TaskCount--; 
       } 
       Intent i = new Intent(ACTION_ON_GET_IMAGE); 
       ImageBox.mQ.offer(b); 
       sendBroadcast(i); 
       if(TaskCount<=0)stopSelf(); 
      } 
     }); 
} 

BroadcastReceiver、

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      LOG.d(TAG, "BroadcastReceiver get broadcast"); 

      String action = intent.getAction(); 
      if (DownLoadImageService.ACTION_ON_GET_IMAGE.equals(action)) { 
       Bitmap b = ImageBox.mQ.poll(); 
       if(b==null)return; 
       if(mListener!=null)mListener.OnGetImage(b); 
      } 
     } 
}; 

。 他のヘルプが必要な場合は教えてください。

-1

あなたと意図からURIを取得する必要があり、

   Uri uri = data.getData(); 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       String imagePath = cursor.getString(columnIndex); 
       Bitmap imageBitmap = BitmapFactory.decodeFile(imagePath); 
       cursor.close(); 
+0

これは、 'Uri'が' MediaStore'からのものである場合にのみ動作し、 'MediaStore'がパスを持っている場合にのみ、そして*そのパスがアプリケーションが読み込みアクセス権を持つ場所(例えば、リムーバブルストレージではない)である場合にのみ* then *を返します。 – CommonsWare

+0

私が間違っている場合は私を訂正してください。ギャラリーから画像を取得するには、アプリにストレージへの読み取りアクセス権が必要ですか? –

+0

アプリに 'READ_EXTERNAL_STORAGE'が必要です。それはリムーバブルストレージをカバーしていません。 「MediaStore」から入手し使用された「Uri」は、メディアメーカーが提供するリムーバブルストレージにアクセスできる* MediaStore *に依存しています。あなたのケースでは、あなたはそれをバイパスしており、アプリはリムーバブルストレージ(Android 4.4以降)にアクセスすることはできません。 – CommonsWare