如何从ListView上的sd卡显示图像

时间:2015-08-12 04:03:17

标签: android listview android-listview

我在活动1中有EditTextListViewImageView

当用户在EditText(活动2)上写入数字(n)ListView =>并点击Ok =​​>显示(n)来自Sd卡的图像时。

在活动2上,当用户点击ImageView =>打开并从Sd卡中选择图片并进行更改时。但我有错误。有人帮帮我,

活动2

public class ListImage extends ActionBarActivity {
String textEncode = null;
ListView list;
ImageView[] image;
String[] a={"image","image1","image2"};

static final int  INTENT_REQUEST_CODE_BROWSE_PICTURE = 1;
static final int  INTENT_REQUEST_CODE_BROWSE_PICTURE1 = 2;
static final int  INTENT_REQUEST_CODE_BROWSE_PICTURE2 = 3;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_image);
    list=(ListView) findViewById(R.id.list);
    sharedPreferences = getSharedPreferences("Reg", Context.MODE_PRIVATE);
    int c=sharedPreferences.getInt("numImage", 0);
     getData(c)
    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            switch (position) {
                case 0:
                    startActivityForResult(intent,
                            INTENT_REQUEST_CODE_BROWSE_PICTURE);
                    break;
                case 1:
                    startActivityForResult(intent,
                            INTENT_REQUEST_CODE_BROWSE_PICTURE1);
                    break;
                case 2:
                    startActivityForResult(intent,
                            INTENT_REQUEST_CODE_BROWSE_PICTURE2);
                    break;
            }
            return true;
        }
    });
}
public void getData(int a){
    String[] item=new String[a];
    Integer[] img=new Integer[a];
    for (int i=0;i<a;i++){
        item[i]="Demo";
        ImageShow adapter=new ImageShow(this,item,img);
        list.setAdapter(adapter);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case INTENT_REQUEST_CODE_BROWSE_PICTURE:
            if( resultCode == Activity.RESULT_OK ){
                execute(data,a[0]);
            }
        case INTENT_REQUEST_CODE_BROWSE_PICTURE1:
            if( resultCode == Activity.RESULT_OK ){
                execute(data,a[1]);
            }
        case INTENT_REQUEST_CODE_BROWSE_PICTURE2:
            if( resultCode == Activity.RESULT_OK ){
                execute(data,a[3]);
            }
}
}

public void execute(Intent intent,String a){
    InputStream stream;
    try {
        stream = getContentResolver().openInputStream(intent.getData());
        Bitmap realImage = BitmapFactory.decodeStream(stream);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
        SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(ListImage.this);
        SharedPreferences.Editor edit=shre.edit();
        edit.putString(a , encodedImage);
        edit.commit();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
private class ImageShow extends ArrayAdapter<String>{

    private ListImage  context;
    private String[] item;
    private Integer[] img;
    public ImageShow(ListImage context,String[] item,Integer[] img) {
        super(context, R.layout.mylist,item);
        this.context=context;
        this.item=item;

        this.img=img;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.mylist,null,true);
        TextView txtTitle=(TextView) rowView.findViewById(R.id.item);
        image=new ImageView[3];
         image[0]=(ImageView) rowView.findViewById(R.id.icon);
        SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(ListImage.this);
        textEncode = shre.getString(a[0],"");
        byte[] b = Base64.decode(textEncode, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
        image[0].setImageBitmap(bitmap);
        TextView extratxt=(TextView) rowView.findViewById(R.id.textView100);
        extratxt.setText("Gioi Thiệu: " + item[position]);
        return rowView;
    }
}
}

1 个答案:

答案 0 :(得分:0)

你是否在清单中包含了阅读SD卡的许可?

  

机器人:名称=&#34; android.permission.READ_EXTERNAL_STORAGE&#34;