如何正确地将照片写入内存并获取URI(Honeycomb)?

发布于 2024-12-18 15:45:33 字数 1760 浏览 5 评论 0原文

我正在构建一个可以拍照然后通过电子邮件发送的应用程序。为此,我必须将其保存到设备的内存中。我遇到的问题是,当应用程序写入我的文件时,它会在这行特定的代码处崩溃:

    intent.putExtra(MediaStore.EXTRA_OUTPUT,    Uri.fromFile(Environment.getExternalStorageDirectory()));

降低你拥有整个函数和代码:

    private void takePicture(){
    File outputFile = new File(Environment.getExternalStorageDirectory(), "image.jpg");
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,    Uri.fromFile(Environment.getExternalStorageDirectory()));
    startActivityForResult(intent, 1);
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
       if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK){
           picture = (Bitmap) data.getExtras().get("data");
           pictureView.setImageBitmap(picture);

           ContentValues values = new ContentValues();
           values.put(Images.Media.TITLE, "Picture");
           values.put(Images.Media.BUCKET_ID, "picture_ID");
           values.put(Images.Media.DESCRIPTION, "");
           values.put(Images.Media.MIME_TYPE, "image/jpeg");

           pictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
           OutputStream outstream;

           try{
               outstream = getContentResolver().openOutputStream(pictureUri);
               picture.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
               outstream.close();
           }catch(FileNotFoundException e){

           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       }
   }

如果我注释掉该行,应用程序就不会再崩溃。我需要获取照片的 URI,以便通过电子邮件发送。

如果有经验丰富的人提供帮助,我们将不胜感激。

I'm building an app that takes a photo and then e-mails it. For this I have to save it into the device's memory. The problem I'm getting is that while the app writes my file, it crashes at this particular line of code:

    intent.putExtra(MediaStore.EXTRA_OUTPUT,    Uri.fromFile(Environment.getExternalStorageDirectory()));

Lower you have the entire function and code:

    private void takePicture(){
    File outputFile = new File(Environment.getExternalStorageDirectory(), "image.jpg");
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,    Uri.fromFile(Environment.getExternalStorageDirectory()));
    startActivityForResult(intent, 1);
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
       if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK){
           picture = (Bitmap) data.getExtras().get("data");
           pictureView.setImageBitmap(picture);

           ContentValues values = new ContentValues();
           values.put(Images.Media.TITLE, "Picture");
           values.put(Images.Media.BUCKET_ID, "picture_ID");
           values.put(Images.Media.DESCRIPTION, "");
           values.put(Images.Media.MIME_TYPE, "image/jpeg");

           pictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
           OutputStream outstream;

           try{
               outstream = getContentResolver().openOutputStream(pictureUri);
               picture.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
               outstream.close();
           }catch(FileNotFoundException e){

           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       }
   }

If I comment that line out, the app doesn't crash anymore. I need to get the photo's URI so I can then email it.

Any help from you guys more experienced out there would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2024-12-25 15:45:33

试试这个

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File path = new File("/mnt/sdcard/YourDirectory");
path.mkdirs();
String fileName = "yourfile.jpg";
File file = new File(path, name);
camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
startActivityForResult(camera, PICTURE_RESULT);



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (PICTURE_RESULT == requestCode && Activity.RESULT_OK == resultCode) {
    if (android.os.Environment.getExternalStorageState().equals(
                 android.os.Environment.MEDIA_MOUNTED)) {
            //////////   TASK TODO After result    //////////
        } else {
        Toast.makeText(this, "sdcard not available", Toast.LENGTH_LONG).show();
        }
    } 
}

Try this

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File path = new File("/mnt/sdcard/YourDirectory");
path.mkdirs();
String fileName = "yourfile.jpg";
File file = new File(path, name);
camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
startActivityForResult(camera, PICTURE_RESULT);



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (PICTURE_RESULT == requestCode && Activity.RESULT_OK == resultCode) {
    if (android.os.Environment.getExternalStorageState().equals(
                 android.os.Environment.MEDIA_MOUNTED)) {
            //////////   TASK TODO After result    //////////
        } else {
        Toast.makeText(this, "sdcard not available", Toast.LENGTH_LONG).show();
        }
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文