【问题标题】:Android Camera Application: Saving and Displaying BitmapsAndroid 相机应用程序:保存和显示位图
【发布时间】:2015-02-26 03:17:05
【问题描述】:

我正在开发此相机应用程序,但无法显示用户拍摄的图像。相机、预览和捕获按钮在主要活动上完美运行。现在,应用程序将照片保存到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;


}

【问题讨论】:

标签: java android android-activity camera internal


【解决方案1】:

试试这个,

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,您可以获得从相机捕获的位图

【讨论】:

  • 但是openFileOutput方法去哪里了?
【解决方案2】:

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

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

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

【讨论】:

  • mediaStorageDir 未初始化。我不知道要制作什么目录....
  • 哎呀,我将编辑代码,但是,您不必设置 mediaStorageDir,它将存储在您的私人目录中(类似于 mycameraapp.myapplication)您只需提供文件名
猜你喜欢
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多