2016-11-15 43 views
0

在我的Android應用程序中,我從設備庫中檢索圖像並設置爲新活動的背景。從庫中檢索圖像將Imageview設置爲背景的問題android

代碼:

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路徑並將其設置爲如上所示。 請確保您有第一

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

確保您設置此圖像視圖作爲背景視圖在烏拉圭回合佈局

試試這個辦法如果你不能找到更好的東西

<?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>
以下權限

now onStart of the activity,i first get the uri from in帳篷,然後

imageView.setImageUri(Uri) 
+0

path_image是一個字符串值。如何設置該字符串值爲setBackgroundResourse() – krishna

+0

使用Uri.parse(字符串)將其解析爲uri,然後將該uri設置爲 – Ak9637

+0

以上的答案,但setBackgroundResourse()的int參數爲 – krishna

0

我做到這一點的方法是:

圖片瀏覽:

<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; 
    } 
} 

可能不是最好的方法,但它適用於我:) 獲取getBitmapFromUri()方法將縮小實際的位圖,以便您不會出現內存異常!

+0

嗨,偉大的解決方案@ kushal,以及我想問你是否將圖像設置爲前景圖像到視圖而不是背景? – Ak9637

+0

您可以將其設置爲您希望的任何圖像視圖。你可以做的是有一個父FrameLayout和第一個孩子是這個圖像視圖和第二個孩子,你看與內容。 –

+0

我已經更新了我的ans框架佈局示例。如果這是有道理的 –

相關問題