2016-11-15 2 views
0

私のアンドロイドアプリケーションでは、デバイスギャラリーからイメージを取得して、新しいアクティビティの背景として設定しています。ギャラリーから画像を取得ImageViewをアンドロイドの背景として設定しているときの問題

コード:

btn_select.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 
     } 
    }); 

} 

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

    if (requestCode == requestCode && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

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

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     Intent i=new Intent(getApplicationContext(),DisplayActivity.class); 
     Bundle bundle=new Bundle(); 
     bundle.putString("path",picturePath); 
     i.putExtras(bundle); 
     startActivity(i); 
     cursor.close(); 

    } 
} 

はその後、新たな活動の背景として設定:

<?xml version="1.0" encoding="utf-8"?> 
    <FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <ImageView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/dis_img" 
      android:adjustViewBounds="true" 
      /> 

    </FrameLayout> 


    dis_img= (ImageView) findViewById(R.id.dis_img); 

    dis_img.setImageURI(Uri.parse(path_image); 

しかし、アプリケーションの実行中ImageViewのは、活動の真ん中に示していません背景画像として表示される。

私はこの画像を行動の背景として使用することはできますか?

+0

実際のレイアウトを確認できるようにスクリーンショットを添付してください。また、代わりに実際のFrameLayoutの背景を設定してみましたか?また、パフォーマンス上の考慮からGlideやメモリの問題を扱う画像読み込みライブラリを使用する方が良いかもしれません。 JPEGは無責任に使用されると多くのメモリを占有します。 –

答えて

0
dis_image.setBackgroundResourse(path_image) 

この写真の意図と同じ活動でdis_imageビュー場合は、別のアクティビティへのURIを取りたいとそこに背景を設定している場合、あなたは、単に意図してURIを渡すことができた背景として画像を設定します

Intent i=new Intent(thisActivity.this,Nextactivity.class);i.putExtra("imageUri",path_image.toString());startActivity(i) 

次いで

Intent intent = getIntent();Uri uri = Uri.parse(intent.getStringExtra("id");dis_image.setBackgroundResourse(uri) 

た後、次の活動のようにuが単純な意図からこのURIのパスを取得し、上記のように設定することができます。 あなたはもっと良いもの

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:orientation="vertical" android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 
<ImageView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:id="@+id/imageView"/> 
 
     
 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:background="@android:color/transparent"> 
 
     
 
     //Now i design all my layout here 
 
     
 
    </LinearLayout> 
 
</RelativeLayout>
を見つけるカントのIF

は、このアプローチをお試しくださいあなたはウルのレイアウトに背景ビューとして、この画像ビューを設定してください

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

最初に次の権限を持っていることを確認してください

今すぐアクティビティの開始、私は最初からuriを取得します10トン、その後

imageView.setImageUri(Uri) 
+0

path_imageは文字列値です。この文字列値をsetBackgroundResourse()に設定する方法 – krishna

+0

Uri.parse(文字列)を使用してURIを解析し、そのURIを上記の – Ak9637

+0

のように解析します。しかし、setBackgroundResourse()はintパラメータ – krishna

0

私はこれを行う方法は次のとおりです。

イメージビュー:Activityクラスで

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    ... 
    > 

    <ImageView 
     android:id="@+id/backdrop" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:adjustViewBounds="true" 
     android:contentDescription="@string/background_image" 
     android:scaleType="centerCrop" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <com.google.android.gms.ads.AdView 
      ... 
     /> 

     <android.support.v7.widget.RecyclerView 
      ... 
     /> 
    </RelativeLayout> 

    <ProgressBar 
     ... 
    /> 

    <com.flipkart.chatheads.ui.ChatHeadContainer 
     ... 
    /> 
</FrameLayout> 

ImageView backdropView = (ImageView) findViewById(R.id.backdrop); 
assert backdropView != null; 
backdropView.setImageBitmap(HelperMethods.getBitmapFromUri(uri, this)); 

ヘルパークラス:

/** 
* This method returns bitmap for a specified URI 
* 
* @param uri URI of image to be converted to bitmap 
* @param mAct Context of activity 
* @return Bitmap 
*/ 

public static Bitmap getBitmapFromUri(Uri uri, Activity mAct) { 

    InputStream in; 
    try { 
     final int IMAGE_MAX_SIZE = 800000; // 0.8MP 
     in = mAct.getContentResolver().openInputStream(uri); 

     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(in, null, o); 
     in.close(); 

     int scale = 1; 
     while ((o.outWidth * o.outHeight) * (1/Math.pow(scale, 2)) > 
       IMAGE_MAX_SIZE) { 
      scale++; 
     } 

     Bitmap b; 
     in = mAct.getContentResolver().openInputStream(uri); 
     if (scale > 1) { 
      scale--; 
      // scale to max possible inSampleSize that still yields an image 
      // larger than target 
      o = new BitmapFactory.Options(); 
      o.inSampleSize = scale; 
      b = BitmapFactory.decodeStream(in, null, o); 

      // resize to desired dimensions 
      int height = b.getHeight(); 
      int width = b.getWidth(); 
      double y = Math.sqrt(IMAGE_MAX_SIZE 
        /(((double) width)/height)); 
      double x = (y/height) * width; 

      Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
        (int) y, true); 
      b.recycle(); 
      b = scaledBitmap; 

      System.gc(); 
     } else { 
      b = BitmapFactory.decodeStream(in); 
     } 
     in.close(); 
     return b; 
    } catch (IOException e) { 
     return null; 
    } 
} 

良い方法ではないかもしれませんが、それは私にとってはうまくいくでしょう:) get getBitmapFromUri()メソッドは実際のビットマップを縮小し、メモリ例外を取り除きません!

+0

こんにちは、kushalのすてきな解決策ですが、これは背景の代わりにビューにフォアグラウンドイメージとしてイメージを設定するかどうか尋ねたいと思いますか? – Ak9637

+0

これは任意の画像表示に設定できます。あなたができることは、親のFrameLayoutを持ち、第1の子供はこの画像ビューであり、第2の子供はコンテンツで見ることです。 –

+0

フレームレイアウトの例で私のansを更新しました。それが意味をなさない場合 –

関連する問題