繁体   English   中英

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

[英]How to save/retrieve a bitmap's file path using Shared Preferences

因此,伙计们,我正在尝试通过SharedPreferences保存位图图像的路径,如果用户之前已经在应用程序上拍摄过照片,则必须在“ onCreate”上进行检索

这是打开的摄像头和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,然后在ononCreate上将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

您尚未初始化editor ,因此出错。 初始化prefs后,必须在onCreate方法中对其进行初始化

prefs = getApplicationContext().getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
editor = prefs.edit();

像这样。

还要在stackoverflow上检查what is NullPointerException in java 您将获得详细信息。

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM