2016-12-13 12 views
0

私はこれでかなり混乱しています。私のコードAndroidで画像を選択

// Read bitmap 
public Bitmap readBitmap(Uri selectedImage) { 
    Bitmap bm = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 
    AssetFileDescriptor fileDescriptor = null; 
    try { 
     fileDescriptor = appContext.getContentResolver().openAssetFileDescriptor(selectedImage,"r"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    finally{ 
     try { 
      bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options); 
      fileDescriptor.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return bm; 
} 

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { 
     Uri filePath = data.getData(); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     bitmap = readBitmap(filePath); 
     SuggestImage.setImageBitmap(bitmap); 
    } 
} 

をチェックし、私は互換性のないタイプが必要と言って、ここで

bitmap = readBitmap(filePath); 

をエラーを取得していますint型が、私は入れてint型時には適用できないと言います。助けが必要です

私はその断片のためだと思います。私は問題が私はContentResolverを得ることができないと思うが、私は確信していない

+0

https://github.com/coomar2841/android-multipicker-library –

+1

「ビットマップ」変数とは何ですか? –

+0

ビットマップビットマップ。 –

答えて

0

ギャラリーから画像を選択する必要があるあなたの断片のボタンeditPicがあなたの断片にあるとしましょう。あなたのフラグメントにギャラリーから

editPic.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        // ******** code for crop image 
        i.putExtra("crop", "true"); 
        i.putExtra("aspectX", 100); 
        i.putExtra("aspectY", 100); 
        i.putExtra("outputX", 300); 
        i.putExtra("outputY", 300); 
        try { 
         i.putExtra("return-data", true); 
         startActivityForResult(
           Intent.createChooser(i, "Select Picture"), 0); 
        }catch (ActivityNotFoundException ex){ 
         ex.printStackTrace(); 
        } 

      } 
     }); 

ピックを実装し、onActivityResultについては、以下のコードを使用するには、次のコードを使用し

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(requestCode==0 && resultCode == Activity.RESULT_OK){ 
      try { 
       Bundle bundle = data.getExtras(); 
       Bitmap bitmap = bundle.getParcelable("data"); // use this bitmap 
       img.setImageBitmap(bitmap); //set image to imageView 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

はそれがあなたのために働くことを願っています。

関連する問題