【问题标题】:Allow user to select background from personalize screen允许用户从个性化屏幕中选择背景
【发布时间】:2013-10-20 21:03:45
【问题描述】:

在我的小部件中,我希望用户能够通过从他们的图库中选择一张图片来设置主页的背景,然后该图片也会显示在个性化屏幕上的 imageView 中(这样他们就知道图像看起来像)。 到目前为止,我已经在个性化页面上设置了 imageView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/personalizetextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/customize" 
    android:textSize="30dip"
    android:gravity="center"
    android:layout_marginTop="20dip"/>

<TextView 
    android:id="@+id/personalizetextviewChangeBackground"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/customizebackground"
    android:gravity="center" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pattern1" />

<Button
    android:id="@+id/btnChangeImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/change_background" />

</LinearLayout>

当 btnChangeImage 被选中时,它会将用户引导到画廊,如下所示:

package com.example.awesomefilebuilderwidget;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Personalize extends Activity{
Button button;
ImageView image;
private static final int SELECT_PICTURE = 1;
private String  selectedImagePath;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.personalize);

    addListenerOnButton();

}

public void addListenerOnButton() {

    image = (ImageView) findViewById(R.id.imageView1);

    button = (Button) findViewById(R.id.btnChangeImage);
    button.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, SELECT_PICTURE);
        }

        public void onActivityResult(int requestCode, int resultCode, Intent data)
            {
                if (resultCode == RESULT_OK) {
                    if (requestCode == SELECT_PICTURE)
                    {
                        Uri selectedImageUri = data.getData();
                        selectedImagePath = getPath(selectedImageUri);
                        try {
                            FileInputStream fileis=new FileInputStream(selectedImagePath);
                            BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
                            byte[] bMapArray= new byte[bufferedstream.available()];
                            bufferedstream.read(bMapArray);
                            Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
                            //Here you can set this /Bitmap image to the button background image

                            if (fileis != null) 
                            {
                                fileis.close();
                            }
                            if (bufferedstream != null) 
                            {
                                bufferedstream.close();
                            }
                        } catch (FileNotFoundException e) {                 
                            e.printStackTrace();
                        } catch (IOException e) {                   
                            e.printStackTrace();
                        }               
                    }
                }
            }


        public String getPath(Uri uri) {
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }

    });

}

}

我怎样才能使选择图像时,该图像显示在图像视图中?那么,在 imageview 中设置的任何图像都会成为主文件的背景?

现在在主页上,我只设置了一个默认图像:

android:background="@drawable/pattern1" 

【问题讨论】:

    标签: android android-imageview android-image


    【解决方案1】:

    将您的图像从 ActivityResult 设置为 ImageView:

    image.setImageBitmap(bMap);
    

    保存此图片,以便您可以再次使用它或在您的主要活动中使用它:

    public boolean saveImageToInternalStorage(Bitmap image) {
       try {
          FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
          image.compress(Bitmap.CompressFormat.PNG, 100, fos);
          fos.close();   
          return true;
       } catch (Exception e) {
       return false;
       }
    }
    

    加载你的图片:

    public Bitmap getThumbnail(String filename) {
       Bitmap thumbnail = null;
       try {
          File filePath = context.getFileStreamPath(filename);
          FileInputStream fi = new FileInputStream(filePath);
          thumbnail = BitmapFactory.decodeStream(fi);
       } catch (Exception ex) {
       Log.e("getThumbnail() on internal storage", ex.getMessage());
       }
       return thumbnail;
    }
    

    在您的主页中将加载的图像设置为背景:

    Drawable d = new BitmapDrawable(getResources(),bitmap);
    yourBackgroundView.setBackground(d);
    

    【讨论】:

    • 只是为了确保,我将“保存您的图像”部分和“加载您的图像”部分添加到我的personalize.java 中,对吗?还是将它们保存到我的主要活动中?
    • 您可以将其保存在personalize.java 中,上下文来自活动。由于您的 Personalize 扩展了 Activity,因此您使用“this”而不是上下文。在您的主要活动中使用加载功能,再次使用“this”而不是上下文并将图像设置为背景。查看developer.android.com/guide/topics/data/data-storage.htmlSharedPreferences 以保存您加载的图像文件名,以便您可以再次加载它并确定用户是否已经选择了图像
    • Drawable d = new BitmapDrawable(getResources(),bitmap);我收到一条错误消息,提示“位图”无法解析为变量。
    • 位图是来自“getThumbnail(..)”的位图对象。因此,例如,您可以编写 Drawable d = new BitmapDrawable(getResource(), getThumbnail("yourFileName"));你错过了一些你应该知道的关于 android 编程的 Java 基础知识
    猜你喜欢
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多