【问题标题】:how to start camera intent and save a non-compressed picture如何启动相机意图并保存非压缩图片
【发布时间】:2014-08-10 07:48:41
【问题描述】:

我是一名非常年轻的自学开发人员,我正在开发我的第一个主要项目,该项目需要在按下后启动相机意图,保存用户拍摄的图像并将其显示在自定义对话框中。
我让它工作了,但我将返回的位图存储在 onactivityresult 中,因此图片被压缩并破坏了应用程序的功能。

这是有效的代码:

开始意图:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);

接收 Intent 并将数据发送到对话框:

            Bundle bundle = data.getExtras();
            File file = new File(getCacheDir() + "/app"
                    + System.currentTimeMillis() + ".jpg");
            Bitmap bitmap = (Bitmap) bundle.get("data");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100 /* ignored for PNG */,
                    bos);
            byte[] bitmapdata = bos.toByteArray();

            // write the bytes in file
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fos = new FileOutputStream(file);
                fos.write(bitmapdata);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mdialog.setPic(file.getAbsolutePath());

在自定义对话框中显示图片:

public void setPic(final String mURi) {
    this.mURI = mURi;

    if (mURI != null) {
        hwPic.postDelayed(new Runnable() {

            @Override
            public void run() {
            Drawable d = Drawable.createFromPath(mURI);

                   hwPic.setImageDrawable(d);;
                hwPic.setVisibility(View.VISIBLE);

            }
        }, 1000);
    }
}

这很好用,但由于图片被压缩,图片中任何大小合理的字体都会模糊不清。

这是不起作用的代码:

初始化变量:

private String MURID;

开始意图:

文件 file = new File(getCacheDir() + "/app" + System.currentTimeMillis() + ".jpg");

                            if(!file.exists()){
                                try {
                                    file.createNewFile();
                                } catch (IOException e) {
                                // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                }else{
                                   file.delete();
                                try {
                                   file.createNewFile();
                                } catch (IOException e) {
                                // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                }
                            MURID=file.getAbsolutePath();
                            Intent intent = new Intent(
                                    MediaStore.ACTION_IMAGE_CAPTURE);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT , Uri.parse(MURID));
                            startActivityForResult(intent, 1);

接收意图并发送到 mydialog:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {// camera intent for the dialog picture
        if (resultCode == RESULT_OK) {


            mdialog.setPic(MURID);

        }
    }
}

setpic 保持不变(在对话框中):

public void setPic(final String mURi) {
    this.mURI = mURi;

    if (mURI != null) {
        hwPic.postDelayed(new Runnable() {

            @Override
            public void run() {
            Drawable d = Drawable.createFromPath(mURI);

                   hwPic.setImageDrawable(d);;
                hwPic.setVisibility(View.VISIBLE);

            }
        }, 1000);
    }
}

我没有得到任何回应,logcat 也没有给我任何错误,这似乎是什么问题?任何帮助将不胜感激。

顺便说一句:我希望这也适用于没有 SD 卡的手机。

【问题讨论】:

    标签: android image android-intent uri saving-data


    【解决方案1】:

    第三方相机应用程序无法写入您的getCacheDir(),如果您指向现有文件,有些应用程序可能会感到困惑。改用外部存储:

    package com.commonsware.android.camcon;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import java.io.File;
    
    public class CameraContentDemoActivity extends Activity {
      private static final int CONTENT_REQUEST=1337;
      private File output=null;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File dir=
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    
        output=new File(dir, "CameraContentDemo.jpeg");
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
    
        startActivityForResult(i, CONTENT_REQUEST);
      }
    
      @Override
      protected void onActivityResult(int requestCode, int resultCode,
                                      Intent data) {
        if (requestCode == CONTENT_REQUEST) {
          if (resultCode == RESULT_OK) {
            Intent i=new Intent(Intent.ACTION_VIEW);
    
            i.setDataAndType(Uri.fromFile(output), "image/jpeg");
            startActivity(i);
            finish();
          }
        }
      }
    }
    

    (来自this sample project in this book

    顺便说一句:我希望它也适用于没有 SD 卡的手机。

    External storage 不是removable storage

    【讨论】:

    • MediaStore.Extra_output 保存在 URI 中的图像将提供的目录中的图像保存为 webp 格式,它还在画廊中将文件显示为 jpg 格式,我该如何防止显示仅在我的私人文件夹中作为 webp 而不是在画廊中作为 jpg 格式?
    • @JayDangar:你不能。您正在将拍照工作委托给另一个应用程序。该应用程序的开发人员可以为所欲为。如果您想完全控制拍照工作,请自己动手,直接使用相机 API 或通过封装这些 API 的库(例如,Fotoapparat、CameraKit-Android)。
    猜你喜欢
    • 1970-01-01
    • 2012-10-28
    • 2011-12-04
    • 2012-09-28
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多