上传图片,并将其设置为背景的Andr​​oid [英] upload image and set it as background Android

查看:121
本文介绍了上传图片,并将其设置为背景的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传图片,并将其设置为背景,但我有没有运气。

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.

下面是code上传图片:

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

然后上传:

  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的:

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..


  1. 所以,我怎么把我的上传图像背景填充整个android手机屏幕上。

  1. 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)

更新(仍然没有工作)

 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.

推荐答案

的onActivityResult(),则必须确保文件的路径是正确的。这是我做的。也许你可以给一个尝试。

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();

一旦你得到picturePath,您可以将其保存到共享preference,这样你可以检查和检索,如果有的话。所以,你不必再挑图片。

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

这篇关于上传图片,并将其设置为背景的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆