【问题标题】:Picasso not load images from SharedPreferences after saved毕加索在保存后不从 SharedPreferences 加载图像
【发布时间】:2017-09-14 18:49:34
【问题描述】:

对不起我的英语。 我的应用程序拍了一张照片,在我使用 picasso 加载图像视图并将图像 Uri 保存在 sharedpreferences 中之后。 拍摄或选择图片但关闭活动时图像加载正常 重新打开图像不显示。会发生什么?

private SharedPreferencesManager mSharedPreferencesManager;
private ImageView imageProfile;
private Uri mMediaUri;

private DialogInterface.OnClickListener mDialogClickListenerToNewMessage = new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case 0:
            //Take Picture
            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            mMediaUri = getOutPutMediaUri(AppConstants.MEDIA_TYPE_IMAGE);
            if (mMediaUri == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MyProfileActivity.this);
                builder.setMessage(R.string.problem_accessing_the_memory_of_your_device) 
                .setTitle(R.string.sorry_)
                .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialogMemory = builder.create();
                dialogMemory.show();
            } else {
                takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                startActivityForResult(takePhotoIntent, AppConstants.TAKE_PHOTO_REQUEST);
            }
            break;
        case 1:
            //Choose Picture
            Intent choosePhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
            choosePhotoIntent.setType("image/*");
            startActivityForResult(choosePhotoIntent, AppConstants.PICK_PHOTO_REQUEST);
            break;
        default:
            break;
        }
    }

};

private Uri getOutPutMediaUri(int mediaType) {
    if (isExternalStoregeAvailable()) {
        //get Uri

        //1.- Get the external storage directory
        String appName = getString(R.string.app_name);
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),appName);

        //2.- Create our subdirectory
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdir()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MyProfileActivity.this);
                builder.setMessage(R.string.failed_to_created_directory_) 
                .setTitle(R.string.sorry_)
                .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialogMemory = builder.create();
                dialogMemory.show();
                return null;
            }
        }
        //3.- Create a file name
        File mediaFile;
        Date currentDate = new Date();
        String timesTamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(currentDate);

        //4.- Crate the file
        String path = mediaStorageDir.getPath() + File.separator;
        if (mediaType == AppConstants.MEDIA_TYPE_IMAGE) {
            mediaFile = new File(path + "IMG_" + timesTamp + ".jpg");
        } else if (mediaType == AppConstants.MEDIA_TYPE_VIDEO) {
            mediaFile = new File(path + "VID_" + timesTamp + ".mp4");
        } else {
            return null;
        }
        //5.- Return the file's URI
        return Uri.fromFile(mediaFile);
    } else {
        return null;
    }
}

private boolean isExternalStoregeAvailable(){
    String state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_MOUNTED)) {
        return true;
    } else {
        return false;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        //add file to the Gallery
        if (requestCode == AppConstants.PICK_PHOTO_REQUEST) {
            if (data == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.failed_to_get_a_file_from_the_gallery) 
                .setTitle(R.string.sorry_)
                .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                mMediaUri = data.getData();
                mSharedPreferencesManager.savePhotoUri(mMediaUri.toString());

            }

        } else {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(mMediaUri);
            sendBroadcast(mediaScanIntent);
            mSharedPreferencesManager.savePhotoUri(mMediaUri.toString());
        }

        Picasso.with(MyProfileActivity.this).load(mMediaUri).into(imageProfile);

    } else if (resultCode != RESULT_CANCELED) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.error_adding_file_to_gallery) 
        .setTitle(R.string.sorry_)
        .setPositiveButton(android.R.string.ok, null);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

我在 onResume 方法中的代码

mSharedPreferencesManager =  new SharedPreferencesManager(this);
final String uri = mSharedPreferencesManager.getPhotoUri();

    if (!uri.equals("null")) {
        Uri imageUri = Uri.parse(uri);
        Picasso.with(MyProfileActivity.this).load(imageUri).into(imageProfile);
    }

//Code to get photo uri in sharedpreferences manager
public String getPhotoUri(){
    String photoUri = sharedPreferences.getString(PHOTO_URI, "null");
    return photoUri;
}

if (uri != "null") {
        Uri imageUri = Uri.parse(uri);
        Picasso.with(MyProfileActivity.this).load(imageUri).into(imageProfile, new Callback() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                Toast.makeText(MyProfileActivity.this, "ok", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                Toast.makeText(MyProfileActivity.this, "error", Toast.LENGTH_SHORT).show();
            }
        });
    }

【问题讨论】:

    标签: android image save loading picasso


    【解决方案1】:

    更改您的 if statement
    if (!uri.equals("null"))
    

    到:

    if (uri != null)
    

    更多见下文:

    How to check if my string is equal to null?

    【讨论】:

    • 这不是问题,我用这个
    • 你应该把String photoUri = sharedPreferences.getString(PHOTO_URI, "null");放在第一位
    • if (uri != "null") 到 if (uri != null)
    • allways return error in callback 什么错误?
    • 这是getPhotoUri()
    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多