问题
I am trying to upload an image and set it as a background but I'm having no luck.
So the uploading is working fine but when I try to set the image as a background It does not work.
Here is the code for upload image:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
then upload:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
// textTargetUri.setText(targetUri.toString());
try {
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
BitmapDrawable bit_background = new BitmapDrawable(getResources(), bitmap);
//prof_bg.setVisibility(View.VISIBLE);
prof_bg.setBackground(bit_background); //does not show.
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
XML ImageView:
<ImageView
android:id="@+id/bg_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
At the moment it is not displaying anything nor give me any errors, so I want to ask..
So how do I get I put the upload image to the background filling the whole android phone screen.
How do I save the image on that background even after activity is stopped. (or call the image straight away when the activity begins without clicking the button to select that image again)
Updated (still not working)
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, false);
BitmapDrawable bit_background = new BitmapDrawable(getResources(), scaledBitmap);
prof_bg.setVisibility(View.VISIBLE);
prof_bg.setBackground(bit_background);
Thanks for your time reading, assistant is still needed.
回答1:
1) setBackground fails due to large image size, try to scale it down before setting:
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, false); //set the w x h as you want
BitmapDrawable bit_background = new BitmapDrawable(getResources(), scaledBitmap);
prof_bg.setVisibility(View.VISIBLE);
prof_bg.setBackground(bit_background);
2) Save the targetUri string in SharedPreferences and retrieve it to set when activity begins.
回答2:
OnActivityResult(), you must ensure that the file path is correct. This is what I do. Maybe you can give a try.
Uri targetUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = mContext.getContentResolver().query(targetUri,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Once you get picturePath, you can save it into SharedPreference so that you can check and retrieve it if there is any. So you don't have to pick the picture again.
// to set the background image
BitmapDrawable bb = new BitmapDrawable (null, BitmapFactory.decodeFile(picturePath));
(findViewById(R.id.bg_image)).setImageDrawable(bb);
来源:https://stackoverflow.com/questions/29439490/upload-image-and-set-it-as-background-android