gpt4 book ai didi

java - 如何在应用程序设置中存储图像路径?

转载 作者:行者123 更新时间:2023-12-02 06:11:54 25 4
gpt4 key购买 nike

您好,我已经做到了,在我的应用程序中,用户可以通过单击将他们定向到画廊的按钮来选择个人资料图片。他们选择了照片,这很好,但一旦我退出应用程序或从多任务中删除,个人资料照片就消失了。我该如何保存它?我知道我需要共享首选项,但我不完全确定如何在代码中使用它。我是初学者。

这是我目前在 main.java 上的代码。它显示了我如何使用代码从图库中选择照片但需要保存该照片:

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

{
}

Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});;


Button week1 = (Button) findViewById(R.id.week1button);
Button week2 = (Button) findViewById(R.id.week2button);
//Button Sound
final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);


week1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.timetable.WEEK1"));
}});

week2.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.timetable.WEEK2"));
}
});

};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}




private static int RESULT_LOAD_IMAGE = 1;

@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.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}


}
}

最佳答案

在SharedPreference中保存图像路径

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("profilePic", picturePath);
edit.commit();

下次 Activity 开始时,请检查您的 SharedPreference。如果存在,则设置图像。下面的代码可以写在onCreate()中。

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

关于java - 如何在应用程序设置中存储图像路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21811638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com