2016-11-15 81 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框架布局示例。如果这是有道理的 –

相关问题