在我的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 :(得分: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)
然后下一个活动你可以简单地从意图中检索这个uri路径并如上所示进行设置。 确保您首先拥有以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
确保将此图片视图设置为ur layout
中的背景视图如果您无法找到更好的方法,请尝试这种方法
<?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>
&#13;
现在开始活动,我首先从意图中获取uri然后
imageView.setImageUri(Uri)
答案 1 :(得分: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>
在Activity类中:
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()方法将缩小实际的位图,以便你不会出现内存异常!