从库保存图像钦点为将来使用 [英] saving image picked from gallery for future use

查看:141
本文介绍了从库保存图像钦点为将来使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我一直在寻找了一段时间了。以下code主从机器人图库图片并显示在一个ImageView的。但继承人的东西,每次应用程序被关闭并重新启动,必须​​重新拾起。我想知道我可以编辑以下保存图片为好,在ImageView的。

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,因此code,数据);

    如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!NULL =数据){
        乌里selectedImage = data.getData();
        的String [] filePathColumn = {MediaStore.Images.Media.DATA};

        光标光标= getContentResolver()查询(selectedImage,
                filePathColumn,NULL,NULL,NULL);
        cursor.moveToFirst();

        INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
        字符串picturePath = cursor.getString(参数:columnIndex);
        cursor.close();

        ImageView的ImageView的=(ImageView的)findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.de codeFILE(picturePath));

    }


}
 

解决方案

唯一用户正在采摘是图片的路径。所以,如果你保存的路径,共享preferences,然后每次应用程序启动时,您可以使用您现有的code,但只是改变你在哪里得到的路径:

 字符串picturePath = preferenceManager.getDefaultShared preferences(本).getString(picturePath,);
如果(!picturePath.equals())
{
   ImageView的ImageView的=(ImageView的)findViewById(R.id.imgView);
   imageView.setImageBitmap(BitmapFactory.de codeFILE(picturePath));
}
 

编辑: 这是你可以在OnCreate中使用一个完整的方法:

 字符串picturePath = preferenceManager.getDefaultShared preferences(本).getString(picturePath,);
如果(!picturePath.equals())
{
   ImageView的ImageView的=(ImageView的)findViewById(R.id.imgView);
   imageView.setImageBitmap(BitmapFactory.de codeFILE(picturePath));
}
其他 {
   selectImage();
}
 

在选择图片使用当前的code开始采摘活动,然后在onActivityResult使用: @覆盖 保护无效onActivityResult(INT申请code,INT结果code,意图数据){     super.onActivityResult(要求code,因此code,数据);

 如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!空=数据){
    乌里selectedImage = data.getData();
    的String [] filePathColumn = {MediaStore.Images.Media.DATA};

    光标光标= getContentResolver()查询(selectedImage,
            filePathColumn,NULL,NULL,NULL);
    cursor.moveToFirst();

    INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
    字符串picturePath = cursor.getString(参数:columnIndex);
    preferenceManager.getDefaultShared preferences(本).edit()putString(picturePath,picturePath).commit()。
    cursor.close();

    ImageView的ImageView的=(ImageView的)findViewById(R.id.imgView);
    imageView.setImageBitmap(BitmapFactory.de codeFILE(picturePath));

}
 

Hey i have been looking for a while now. the following code picks the image from the android gallery and shows it in an imageView. but heres the thing, everytime the app is closed and restarted the has to be picked again. i would like to know how i can edit the following to save the image for good in the imageView.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && 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);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }


}

解决方案

The only thing that the user is picking is the path of the picture. So if you save the path to SharedPreferences, then everytime the app is started, you can use your existing code, but just change where you get the path:

String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
   ImageView imageView = (ImageView) findViewById(R.id.imgView);
   imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}

EDIT: This is a complete method you can use in OnCreate:

String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
   ImageView imageView = (ImageView) findViewById(R.id.imgView);
   imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
else {
   selectImage();
}

In select image use your current code to start the picking activity, then in onActivityResult use this: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && 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);
    PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
    cursor.close();

    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}

这篇关于从库保存图像钦点为将来使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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