【问题标题】:Taking a high quality image through default camera activity and saving it o the sd card通过默认相机活动拍摄高质量图像并将其保存到 SD 卡中
【发布时间】:2012-06-15 09:58:00
【问题描述】:

我正在通过默认相机活动(使用intent.put Extras)拍摄一张高分辨率照片,并将其保存到sd卡中,

代码:

public class CameraActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    Button takepicture ;
    ImageView iv ;
    TextView tv;
    Button show;

    String filepath;
    Intent i;
    Uri mUri;

    final static int cameraData = 0;

    File folder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        takepicture = (Button) findViewById(R.id.button1);
        iv = (ImageView) findViewById(R.id.imageView1);
        tv = (TextView) findViewById(R.id.textView1);
        show = (Button) findViewById(R.id.button2);
        takepicture.setOnClickListener(this);
        show.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch(v.getId()){

        case R.id.button1:

            String sdcardstate = android.os.Environment.getExternalStorageState();

            if(sdcardstate.contentEquals(android.os.Environment.MEDIA_MOUNTED)){

                 filepath = Environment.getExternalStorageDirectory().getPath();

                 folder = new File(filepath,"wax");

                 if(!folder.exists()){
                     try {
                        folder.createNewFile();
                         Log.d("folder created", "ya");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                 }

                 mUri = Uri.fromFile(folder);
                 Log.d("bk", mUri.toString());

                 i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                 i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

                 Log.d("extra", "extra");
                 startActivityForResult(i,cameraData);
            }
            break;

        case R.id.button2:

            File f = new File(filepath,"bmp.png");

            Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());              

            iv.setImageBitmap(myBitmap);                
            break;
        }           
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode==RESULT_OK){

            tv.setText("Result ok");
            Log.d("ok", "ok");
            Bundle extras = data.getExtras();

            Bitmap bmp = (Bitmap) extras.get("data");
        }
    }
}

相机活动开始,图像被拍摄,但是当我点击保存时,它没有返回并强制关闭。

我已经阅读了很多关于此的主题,了解到必须在相机活动开始之前创建文件,但仍然没有。

请帮忙,我在这个问题上卡了一个星期左右。

Logcat 错误

06-15 16:05:50.205: W/dalvikvm(1780): threadid=10: thread exiting with uncaught exception (group=0x4001d800)
06-15 16:05:50.205: E/AndroidRuntime(1780): FATAL EXCEPTION: GLThread 12
06-15 16:05:50.205: E/AndroidRuntime(1780): java.lang.IllegalArgumentException: No configs match configSpec
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$BaseConfigChooser.chooseConfig(GLSurfaceView.java:760)
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:916)
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1246)
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
06-15 16:05:50.294: W/ActivityManager(59):   Force finishing activity com.android.camera/.Camera
06-15 16:05:50.444: V/camera(1780): stopPreview

【问题讨论】:

  • 我正在设备上测试它,即使那样我也会放置模拟器 logcat,
  • 你知道你可以在真机上调试应用吗?它比模拟器容易得多:)
  • 我知道,但不知何故,我的电脑上没有安装设备的驱动程序.. 所以帮不上忙
  • 和...你的问题中的“高质量”与此有什么关系?

标签: android android-camera


【解决方案1】:

使用以下方法来实现。

在调用 CameraIntent 之前,根据该文件路径创建一个文件和 uri,如下所示。

filename = Environment.getExternalStorageDirectory().getPath() + "/test/testfile.jpg";
imageUri = Uri.fromFile(new File(filename));

// start default camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                imageUri);
startActivityForResult (cameraIntent, CAMERA_PIC_REQUEST);

现在,您有了可以在 onAcityvityResult 方法中使用的文件路径,如下所示,

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode != CAMERA_PIC_REQUEST || filename == null)
        return;
    ImageView img = (ImageView) findViewById(R.id.image);
    img.setImageURI(imageUri);
}

【讨论】:

  • 太棒了,我之前试过这个,它对我没有用,但它确实做到了,我不知道是什么错误,但没关系,它工作干杯!!;)
  • 还有一件事,当将图像设置为 imageview 时,它设置在风景中,而我希望它与之相反,我该如何实现?
  • 即使我以纵向模式拍摄,图像实际上也保存在风景中
【解决方案2】:

这闻起来像 outOfMemoryException。而不是直接获取巨大的图片文件,您需要做一些代码魔术,这样它就不会占用所有内存。在此处查看一些文档:http://developer.android.com/training/displaying-bitmaps/index.html

还有一些代码 4 u:

public Bitmap decodeFile(File f, int size){
    try {

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;

        while(true){
            if(width_tmp/2<size) // || height 
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

【讨论】:

    【解决方案3】:

    尽量把所有代码都放在各自的Try Catch Exception处理中。然后调试代码并检查抛出异常的位置。您可能是内存不足异常。

    按照这个:

    http://developer.android.com/training/camera/photobasics.html

    【讨论】:

    • 你的意思是因为高质量图像的大小,可能会导致内存错误??
    • 也许吧。这可能是一个原因。
    • 难道我们没有任何办法可以从默认的相机活动中获得高质量的图像并将其保存到 SD 卡中。请试一试
    【解决方案4】:
    public class MainActivity extends AppCompatActivity {
    
        private static final int CAMERA_REQUEST = 1888;
        private ImageView imageView;
        private LinearLayout view;
        private TextView photoButton, shareButton, saveButton;
        private Bitmap b, photo;
        private static final int MY_CAMERA_PERMISSION_CODE = 100;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            view = findViewById(R.id.view);
            imageView = findViewById(R.id.image);
            photoButton = this.findViewById(R.id.captur);
    
            photoButton.setOnClickListener(new View.OnClickListener() {
    
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(View v) {
                    if (checkSelfPermission(Manifest.permission.CAMERA)
                            != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(new String[]{Manifest.permission.CAMERA},
                                MY_CAMERA_PERMISSION_CODE);
                    } else {
                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    
                    }
                }
            });
    
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if (requestCode == MY_CAMERA_PERMISSION_CODE) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                    Intent cameraIntent = new
                            Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                } else {
                    Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
                }
    
            }
        }
    
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
                photo = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(photo);
                //convert bitmap
                view.setDrawingCacheEnabled(true);
    
                view.buildDrawingCache(true);
                b = Bitmap.createBitmap(view.getDrawingCache());
                view.setDrawingCacheEnabled(false);
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      相关资源
      最近更新 更多