如何使用共享首选项保存/检索位图的文件路径

时间:2016-03-09 02:32:35

标签: java android

所以,我试图通过SharedPreferences保存位图图像的路径,如果用户之前已经在应用上拍了照片,我必须在“创建”#39上检索它;

所以这里是开放式摄像头和onActivityResult代码

public void abrirCamera(View v){
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
    startActivityForResult(cameraIntent, RESULT_FIRST_USER);
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_FIRST_USER){
        Bitmap image = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(image);
        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        Uri tempUri = getImageUri(getApplicationContext(), image);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
        File finalFile = new File(getRealPathFromURI(tempUri));

        outputFileUri = Uri.fromFile(finalFile);

        stringUri = outputFileUri.toString();

        editor.putString("imagepath", stringUri);
        editor.commit();


    }
}

这是onCreate:

public class MainActivity extends AppCompatActivity {

public static final String PREFS_NAME = "MyPref";
EditText editWeight;
EditText editHeight;
RatingBar rb1;
SharedPreferences prefs;
SharedPreferences.Editor editor;
float i;
Uri outputFileUri;
Uri uriString;
String stringUri;
ImageView imageView;  
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editWeight = (EditText) findViewById(R.id.editWeight);
    editHeight = (EditText) findViewById(R.id.editHeight);
    imageView = (ImageView) findViewById(R.id.imageView1);
    rb1 = (RatingBar) findViewById(R.id.rb1);


    prefs = getApplicationContext().getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    String restoredText = prefs.getString("text", "");
    if (restoredText.equals("")){
        float peso = prefs.getFloat("peso", 0);
        float altura = prefs.getFloat("altura", 0);
        editWeight.setText(String.valueOf(peso));
        editHeight.setText(String.valueOf(altura));
        float rating = prefs.getFloat("rating", 0);
        rb1.setRating(Float.parseFloat(String.valueOf(rating)));
        String imagepath = prefs.getString("imagepath", "");
        uriString = Uri.parse(imagepath);
        imageView.setImageURI(uriString);

    }
}

我试图通过String发送到我的sharedPreferences然后在' onCreate'将String转换为Uri,因此我可以在ImageView上设置图像。

这就是错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/5047 (has extras) }} to activity {com.example.casa.myimc/com.example.casa.myimc.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference

引起:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference

1 个答案:

答案 0 :(得分:1)

您尚未初始化prefs,因此错误。初始化prefs = getApplicationContext().getSharedPreferences(PREFS_NAME, MODE_PRIVATE); editor = prefs.edit();

后,您必须在what is NullPointerException in java方法中对其进行初始化
ltrim()

喜欢这个。

同时在stackoverflow上检查array_map()。您将获得详细信息。

希望有所帮助。