2017-12-15 9 views
0

私は、ギャラリーから画像を選択し、インテントを使って画像をトリミングすることができるようにするコードを作成しようとしています(プロファイル画像用)。私の切り抜かれたイメージはなぜ少しですか?

すべてがうまくいき、画像を選択できます。カスタムダイアログにあるImageViewにクロップして表示できます。私のカスタムダイアログのImageViewは幅と高さにwrap_contentを設定してもほとんどないので、ユーザーは切り抜かれた画像をほとんど見ることができません。ここで

はスクリーンショットです:Little image

コード:

私のカスタムダイアログ(custom_dialog_pp.xml):私のMainActivityで

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/style_layout_rounded_white"> 


<TextView 
    android:id="@+id/useit_customdialog_pp_TV" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:gravity="center" 
    android:text="@string/usethispic" 
    android:textSize="25sp" 
    android:textColor="@color/white" 
    android:background="@drawable/style_tv_toproundcorner"/> 

<ImageView 
    android:contentDescription="@string/profilpic" 
    android:id="@+id/pp_customdialog_pp_IV" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

/> 

<Space 
    android:layout_width="match_parent" 
    android:layout_height="10dp" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dp"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:id="@+id/cancel_customdialog_pp_BTN" 
     android:background="@drawable/style_button_primary" 
     android:text="@string/cancel" 
     /> 

    <Space 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:id="@+id/done_customdialog_pp_BTN" 
     android:background="@drawable/style_button_primary" 
     android:text="@string/done" 
     /> 

</LinearLayout> 

//////////////////// 
ChangeProfilPic.onClickListener { 
Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(galleryIntent, 2); 
} 
//////////////////// 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == 4 && resultCode == RESULT_OK && data != null) { 

     Bundle extras = data.getExtras(); 
     Bitmap selectedImage = extras.getParcelable("data"); 


     android.support.v7.app.AlertDialog.Builder mBuilderLoading = new android.support.v7.app.AlertDialog.Builder(MainActivity.this); 
     View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null); 

     final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV); 

     pp_customdialog_pp_IV.setImageBitmap(selectedImage); 


     mBuilderLoading.setView(mViewLoading); 
     final android.support.v7.app.AlertDialog dialogLoading = mBuilderLoading.create(); 
     dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
     dialogLoading.show(); 


    } else if(requestCode == 2 && resultCode == RESULT_OK && data != null) { 
     ImageCropFunction(data.getData()); 
    } 
} 


public void ImageCropFunction(Uri imguri) { 
    try { 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     cropIntent.setDataAndType(imguri, "image/*"); 
     cropIntent.putExtra("crop", "true"); 
     cropIntent.putExtra("aspectX", 1); 
     cropIntent.putExtra("aspectY", 1); 
     cropIntent.putExtra("outputX", 128); 
     cropIntent.putExtra("outputY", 128); 
     cropIntent.putExtra("return-data", true); 
     startActivityForResult(cropIntent, 4); 

    } catch (ActivityNotFoundException ignored) { 

    } 

Ps:スクロッキングオプションを維持したい

+0

「outputX」と「outputY」は「128」です。 128x128に戻った画像はありますか?たぶんそれは問題です –

+0

@ th3pat3lそれは私に128または1000または50000を置くと同じ結果を与えます。 –

+0

私の答えは以下を見てください。これを行うと、その '128'をより高い数字に変更しなければなりません。 512x512のようなサイズを試してください –

答えて

0

onActivityResult()をこのように変更してください。

戻ってくるのはサムネイル(128x128)です。フルイメージを取得するには、Uriを使用する必要があります。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == 2 && resultCode == RESULT_OK && data != null) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     if (selectedImage == null) { 
      return; 
     } 

     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 

     if (cursor == null) { 
      return; 
     } 

     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 


     if (picturePath != null) { 
      Log.d("TAG", "picturePath " + picturePath); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options); 

      if(bitmap != null) { 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       // you can change the quality here. for dialog, you might only want 50 instead of 100.. 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 

       AlertDialog.Builder mBuilderLoading = new AlertDialog.Builder(MainActivity.this); 
       View mViewLoading = getLayoutInflater().inflate(R.layout.custom_dialog_pp,null); 

       final ImageView pp_customdialog_pp_IV = mViewLoading.findViewById(R.id.pp_customdialog_pp_IV); 
       pp_customdialog_pp_IV.setImageBitmap(bitmap); 
       mBuilderLoading.setView(mViewLoading); 

       final AlertDialog dialogLoading = mBuilderLoading.create(); 
       dialogLoading.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
       dialogLoading.show(); 
      } else { 
       Log.e("TAG", "bitmap is null?!"); 
      } 
     } 
    } 
} 
+0

私のUri selectedImageは常に== nullです...なぜですか? –

+0

ああ!私は理由を見る。あなたは 'startActivityForResult(galleryIntent、2);で' 2'の値を送ります。したがって、if文はif(requestCode == 2 ...)に変更する必要があります。私は私の答えを編集しました。試してみてください –

+0

@MelvinAuvray私は上記の例を試す機会がありました。それは完全に動作します –

0

私はデフォルトの動作が返品結果にサムネイルを返すと考えています。そのURIを使用して、返品時にフルサイズの画像を取得する必要がある場合があります。

関連する問題