Android Camera Application:保存和显示位图

时间:2015-02-26 03:17:05

标签: java android android-activity camera internal

我正在使用此相机应用程序,但我无法显示用户拍摄的图像。相机,预览和捕捉按钮完美地适用于主要活动。现在,应用程序将照片保存到Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp"但我不希望图像保存在那里。我只想将该图像传递给下一个活动,(不将其保存到公共目录)。有没有办法做到这一点?我查看了教程,但无法弄清楚如何实现MODE.PRIVATE。

以下是它的名称:

  @Override
    public void onPictureTaken(final byte[] data, Camera camera) {

        new AsyncTask<Void, Void, String>() {

            @Override
            protected String doInBackground(Void... params) {
                File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                if (pictureFile == null){
                    Log.d(TAG, "Error creating media file, check storage permissions: ");
                    return null;
                }

                try {
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                    return pictureFile.getPath();
                } catch (FileNotFoundException e) {
                    Log.d(TAG, "File not found: " + e.getMessage());
                    return null;
                } catch (IOException e) {
                    Log.d(TAG, "Error accessing file: " + e.getMessage());
                    return null;
                }
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                if(result != null){
                    Intent intent = new Intent(mContext, ImageDisplayActivity.class);
                    intent.putExtra(ImageDisplayActivity.KEY_PATH, result);
                    startActivity(intent);
                }
            }

        }.execute();


    }

以下是该方法的代码:

private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new     File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
      // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",    Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;


}

2 个答案:

答案 0 :(得分:0)

试试这个,

ActivityOne.Java

try {
    //Write file
    String filename = "photo.png";
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
    bmp.compress(Bitmap.CompressFormat.PNG, 200, stream);

    //Cleanup
    stream.close();


    //Pop intent
    Intent mIntent= new Intent(this, ActivityTwo.class);
    in1.putExtra("bitmap", filename);
    startActivity(mIntent);
} catch (Exception e) {
    e.printStackTrace();
}

<强> ActivityTwo.Java

Bitmap bmp = null;
String filename = getIntent().getStringExtra("bitmap");
try {
    FileInputStream is = this.openFileInput(filename);
    bmp = BitmapFactory.decodeStream(is);
    is.close();
} catch (Exception e) {
    e.printStackTrace();
}

并使用位图设置Imageview,您可以获取从相机中捕获的位图

答案 1 :(得分:0)

  

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

看一下例子:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

基本上,你只需要一个FILENAME就可以在私有模式下创建一个文件。 只保留你的方法getOutputMediaFile(int type)的最后一部分它应该是这样的:

private static File getOutputMediaFile(int type){
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                                              Locale.getDefault()).format(new Date());

     if (type == MEDIA_TYPE_IMAGE) {
          mediaFile =  "IMG_"+ timeStamp + ".jpg";
       } else {
          return null;
       }
      return mediaFile;  
}

然后只需更改doInBackground function的尝试:

    try {
      FileOutputStream fos = openFileOutput(pictureFile, Context.MODE_PRIVATE);
      fos.write(data);
      fos.close();
      return pictureFile;
     } 

只需提供其他活动的路径并创建一个bmp

String filepath = intent.getStringExtra("EXTRA_FILE");
java.io.FileInputStream in = openFileInput(filepath);
Bitmap bmp = BitmapFactory.decodeStream(in)