如何从图像捕获中获取结果并将其保存到 Android 中的共享首选项
How to get a Result from Image Capture and save it to Shared Preferences in Android
我正在构建一个应用程序,当用户单击 imageview 时,我希望设备相机应用程序启动并拍照并将其设置到 imageview。我做到了!!!但是当我退出应用程序并重新打开它时,图像已被删除。我怎样才能使图像永久化?
我的代码:
private SessionManager session;
private static final int REQ_CODE = 1152;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private Uri file;
private String fileName = "ImageTaken";
private ImageView imagePhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
session = new SessionManager(getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
Toolbar tb = (Toolbar)findViewById(R.id.topBar);
setSupportActionBar(tb);
String name = user.get(SessionManager.KEY_NAME);
TextView watersName = (TextView)findViewById(R.id.waitersName);
TextView date = (TextView)findViewById(R.id.date);
TextView time = (TextView)findViewById(R.id.time);
watersName.setText(Html.fromHtml("<b>" + name + "</b>"));
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd" + "/" + "mm" + "/" + "yyyy");
String text = dateFormat.format(c.getTime());
date.setText(text);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
String timeF = timeFormat.format(c.getTime());
time.setText(timeF);
Button btnnewworder = (Button)findViewById(R.id.btnnewworder);
btnnewworder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(UserData.this, Tables.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}
});
Button payment = (Button)findViewById(R.id.btnpayment);
payment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogMessageDisplay.displayInfoMessage(UserData.this, "Payment Details", "There was no order made. \n Make an order and then tap the payment button.", AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}
});
imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
imagePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
file = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//file = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, REQ_CODE);
}
});
}
OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_CODE){
if(resultCode == RESULT_OK){
imagePhoto.setImageURI(file);
editor.putString(fileName, file.toString());
editor.commit();
Toast.makeText(UserData.this, "Your file has been saved at " + file, Toast.LENGTH_SHORT).show();
}else if(resultCode == RESULT_CANCELED){
Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
提前致谢!!!
SharedPreferences
不适用于保存图像等大量数据。它们用于保存有关图像位置或用户选择的其他小选择的信息。
如果您想保存图像,那么您应该这样做。将图像保存在应用程序的本地文件中,稍后再检索它。如果您 return 时愿意,可以将该文件的位置保存在 SharedPreference
对象中。如果只有一个,那么下次拍照时可以用相同文件名的下一个替换它。这样一来,您始终可以获得自上次拍摄以来的最新图像;即使在您关闭并重新启动 phone.
之后
试试这个:
1) 初始化这些:
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
2) 修改你的 onCreate()
如:
sharedPreferences = getSharedPreferences(fileName, MODE_PRIVATE);
editor = sharedPreferences.edit();
imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
String commited = sharedPreferences.getString(fileName, null);
if(commited != null) {
imagePhoto.setImageURI(Uri.parse(commited));
}
3) 修改您的 onActivityResult()
,例如:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_CODE){
if(resultCode == RESULT_OK){
imagePhoto.setImageURI(file);
editor.putString(fileName, file.toString());
editor.commit();
}else if(resultCode == RESULT_CANCELED){
Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
然后你就会得到你想要的效果。希望对你有帮助!!!
我正在构建一个应用程序,当用户单击 imageview 时,我希望设备相机应用程序启动并拍照并将其设置到 imageview。我做到了!!!但是当我退出应用程序并重新打开它时,图像已被删除。我怎样才能使图像永久化?
我的代码:
private SessionManager session;
private static final int REQ_CODE = 1152;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private Uri file;
private String fileName = "ImageTaken";
private ImageView imagePhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
session = new SessionManager(getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
Toolbar tb = (Toolbar)findViewById(R.id.topBar);
setSupportActionBar(tb);
String name = user.get(SessionManager.KEY_NAME);
TextView watersName = (TextView)findViewById(R.id.waitersName);
TextView date = (TextView)findViewById(R.id.date);
TextView time = (TextView)findViewById(R.id.time);
watersName.setText(Html.fromHtml("<b>" + name + "</b>"));
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd" + "/" + "mm" + "/" + "yyyy");
String text = dateFormat.format(c.getTime());
date.setText(text);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
String timeF = timeFormat.format(c.getTime());
time.setText(timeF);
Button btnnewworder = (Button)findViewById(R.id.btnnewworder);
btnnewworder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(UserData.this, Tables.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}
});
Button payment = (Button)findViewById(R.id.btnpayment);
payment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogMessageDisplay.displayInfoMessage(UserData.this, "Payment Details", "There was no order made. \n Make an order and then tap the payment button.", AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}
});
imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
imagePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
file = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//file = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, REQ_CODE);
}
});
}
OnActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_CODE){
if(resultCode == RESULT_OK){
imagePhoto.setImageURI(file);
editor.putString(fileName, file.toString());
editor.commit();
Toast.makeText(UserData.this, "Your file has been saved at " + file, Toast.LENGTH_SHORT).show();
}else if(resultCode == RESULT_CANCELED){
Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
提前致谢!!!
SharedPreferences
不适用于保存图像等大量数据。它们用于保存有关图像位置或用户选择的其他小选择的信息。
如果您想保存图像,那么您应该这样做。将图像保存在应用程序的本地文件中,稍后再检索它。如果您 return 时愿意,可以将该文件的位置保存在 SharedPreference
对象中。如果只有一个,那么下次拍照时可以用相同文件名的下一个替换它。这样一来,您始终可以获得自上次拍摄以来的最新图像;即使在您关闭并重新启动 phone.
试试这个:
1) 初始化这些:
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
2) 修改你的 onCreate()
如:
sharedPreferences = getSharedPreferences(fileName, MODE_PRIVATE);
editor = sharedPreferences.edit();
imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
String commited = sharedPreferences.getString(fileName, null);
if(commited != null) {
imagePhoto.setImageURI(Uri.parse(commited));
}
3) 修改您的 onActivityResult()
,例如:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_CODE){
if(resultCode == RESULT_OK){
imagePhoto.setImageURI(file);
editor.putString(fileName, file.toString());
editor.commit();
}else if(resultCode == RESULT_CANCELED){
Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
然后你就会得到你想要的效果。希望对你有帮助!!!