用户从列表中选择图像后,如何保存图像供以后使用

时间:2013-11-22 02:27:01

标签: android eclipse

我有以下代码:

public class MainActivity extends Activity {

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

        Button camera = (Button) findViewById(R.id.bt_camera);

        camera.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {                
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 0);
            }
        });         
    }
}

代码提示用户从手机中选择和成像。现在我想保存此图像,以便我的应用程序可以裁剪图像。如何保存用户选择的图像?

2 个答案:

答案 0 :(得分:0)

以下是我用来将Image存储到SDCard的方法

public void saveImageToSDCard(byte[] imageDataArray) {

        File sdDir = Environment
                  .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        File pictureFileDir = new File(sdDir, "CameraAPIDemo");

        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

          Log.d("saveImageToSDCard", "Can't create directory to save image.");

          return;

        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        String photoFile = "Picture_" + date + ".jpg";

        String imageFileName = pictureFileDir.getPath() + File.separator + photoFile;

        File pictureFile = new File(imageFileName);

        try {
          FileOutputStream fos = new FileOutputStream(pictureFile);
          fos.write(imageDataArray);
          fos.close();
          Toast.makeText(context, "New Image saved:" + photoFile,
              Toast.LENGTH_LONG).show();
        } catch (Exception error) {
          Log.d("saveImageToSDCard", "File" + imageFileName + "not saved: "
              + error.getMessage());
          Toast.makeText(context, "Image could not be saved.",
              Toast.LENGTH_LONG).show();
        }
    }

答案 1 :(得分:0)

private File file = null;
private String savedPath = null;
private Bitmap bm; 

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == getActivity().RESULT_OK) {

    if (requestCode == 0) { // Recommend to use static int to check
            Uri selectedImageUri = data.getData();

            String tempPath = getPath(selectedImageUri, getActivity());   // get the image path

            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();  
            bm = BitmapFactory.decodeFile(tempPath, btmapOptions); // Now you can use this bitmap

            savedPath = getPath(selectedImageUri, getActivity());
            file = new File(savedPath);
            Log.i("File Name: ", file.getAbsolutePath() + " - " + file.getName());

            imageView.setImageBitmap(bm);  // If you want to show in image view
            imageView.setVisibility(View.VISIBLE); 
        }

    }
}

    public String getPath(Uri uri, Activity activity) {
        String[] projection = { MediaColumns.DATA };
        Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
}