如何从Image Capture获取结果并将其保存到Android中的共享首选项

时间:2015-03-04 20:30:42

标签: android android-camera-intent

我正在构建一个应用程序,当用户单击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();
            }
        }
    }

提前致谢!!!

2 个答案:

答案 0 :(得分:2)

SharedPreferences不适用于保存大量数据,例如图像。它们用于保存有关图像的位置或用户选择的其他小选择的信息。

如果您想要关闭图像,那么您应该这样做。将图像保存在应用程序中的本地文件中,然后再检索。如果您在返回时希望将该文件的位置保存在SharedPreference对象中,则可以将其保存。如果它只有一个,那么下次拍照时可以用相同文件名中的下一个替换它。这样,自上次拍摄照片以来,您始终拥有最新的图像;即使您关闭并重新启动手机也是如此。

答案 1 :(得分:1)

试试这个:

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();
            }
        }
    }

然后你会有你想要的效果。希望它有所帮助!!!