Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MediaStore.EXTRA_输出将数据呈现为空,是否以其他方式保存照片?_Java_Android_Android Intent_Camera - Fatal编程技术网

Java MediaStore.EXTRA_输出将数据呈现为空,是否以其他方式保存照片?

Java MediaStore.EXTRA_输出将数据呈现为空,是否以其他方式保存照片?,java,android,android-intent,camera,Java,Android,Android Intent,Camera,谷歌通过intent提供了这一多功能的拍照代码: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // create Intent to take a picture and return control to the calling application

谷歌通过intent提供了这一多功能的拍照代码:

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

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
问题是,如果您像我一样,希望将照片作为附加内容传递,则使用
EXTRA_OUTPUT
似乎会与照片数据一起运行,并使后续操作认为意图数据为空

这是Android的一个大错误

我正在尝试拍摄一张照片,然后在新视图中将其显示为缩略图。我想在用户的图库中将其保存为全尺寸图像。是否有人知道一种不使用
EXTRA_OUTPUT
指定图像位置的方法

以下是我目前拥有的:

public void takePhoto(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
//  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
@SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "JoshuaTree");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("JoshuaTree", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").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;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            handleSmallCameraPhoto(data);
        }
    }
}


private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    Intent displayIntent = new Intent(this, DisplayPhotoActivity.class);
    displayIntent.putExtra("BitmapImage", mImageBitmap);
    startActivity(displayIntent);
}

}

如果指定了MediaStore.EXTRA\u输出,则拍摄的图像将写入该路径,并且不会向onActivityResult提供任何数据。您可以从指定的内容读取图像


请参阅此处解决的另一个相同问题:

以下是如何实现您的目标:

public void onClick(View v) 
  {

    switch(v.getId())

          {

           case R.id.iBCamera:


            File image = new File(appFolderCheckandCreate(), "img" + getTimeStamp() + ".jpg");
            Uri uriSavedImage = Uri.fromFile(image);

            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            i.putExtra("return-data", true);
            startActivityForResult(i, CAMERA_RESULT);

            break;

           }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode)

          {

          case CAMERA_RESULT:

                       if(resultCode==RESULT_OK)
                          {
                           handleSmallCameraPhoto(uriSavedImage);     
                          }
                     break;

          }

 }

     private void handleSmallCameraPhoto(Uri uri) 
        {
           Bitmap bmp=null;

              try {
                bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                   } 
                   catch (FileNotFoundException e) 
                   {

                 e.printStackTrace();
                }


               Intent displayIntent = new Intent(this, DisplayPhotoActivity.class);
             displayIntent.putExtra("BitmapImage", bmp);
            startActivity(displayIntent);


       }

private String appFolderCheckandCreate(){

    String appFolderPath="";
    File externalStorage = Environment.getExternalStorageDirectory();

    if (externalStorage.canWrite()) 
    {
        appFolderPath = externalStorage.getAbsolutePath() + "/MyApp";
        File dir = new File(appFolderPath);

        if (!dir.exists()) 
        {
              dir.mkdirs();
        }

    }
    else
    {
      showToast("  Storage media not found or is full ! ");
    }

    return appFolderPath;
}



 private String getTimeStamp() {

    final long timestamp = new Date().getTime();

    final Calendar cal = Calendar.getInstance();
                   cal.setTimeInMillis(timestamp);

    final String timeString = new SimpleDateFormat("HH_mm_ss_SSS").format(cal.getTime());


    return timeString;
}
编辑:

将这些添加到清单中:

应明确声明启动Api 19及更高版本的读取外部存储

     *<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />*
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
**

向你证明

基本上,从相机检索图像有两种方法,如果您使用通过intent extras发送捕获的图像,则无法在ActivityResult on intent.getData()上检索,因为它通过extras保存图像数据

因此,我们的想法是:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
并在ActivityResults上检索它检查检索到的意图:

    @Override  
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {        
    if (resultCode == RESULT_OK) {            
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {                
            if (intent.getData() != null) {                    
                ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(intent.getData(), "r");    
                      
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();                      
                Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);                     
                parcelFileDescriptor.close();
            } else {                    
                Bitmap imageRetrieved = (Bitmap) intent.getExtras().get("data");              
            }           
        } 
    }
}

试试这个
android.provider.MediaStore.EXTRA_OUTPUT
希望能解决你的bug。以下代码适用于我:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); 
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

我也面临着同样的问题,每次使用
getIntent().getData()
检索其他活动的数据时,都会得到null值,然后通过编写complete
android.provider.MediaStore.EXTRA输出来解决,这就解决了我的错误。

试试这个takePictureContent.putExtra(“返回数据”,true)@JRowan没有保存的迹象:|将mediaFile设置为类的全局文件,甚至是静态文件(如果需要),那么您将始终保持该文件的就绪。您是否设置了写入外部存储的权限?这很好,但额外的输出只会阻止相机在我的应用程序中拍照。我希望他们扔掉整个摄像头库,尽快编写一个新的。只需添加路径必须以目录结尾。如果你试图指定一个像picture.jpg这样的文件,它将在内部崩溃,并拒绝确认你在相机中点击的ACCEPT_picture按钮。此外,API 21中提供了Camera2,因此很快我们就不必再处理所有这些模糊的东西了!是的,你是对的。但是文档告诉我们getData应该返回图像的URI。事实上,在某些设备(例如摩托罗拉razr)中,您可以将数据取回。是的,在第一点w.r.t时间得到答复时达成一致。但现在您必须从API 19开始读取外部存储。只有在您确实需要SD卡访问时才可以查看(无意冒犯,但不要同意2和3),因为您可以访问沙盒文件结构。至于样式,有太多不必要的空白、新行、未对齐的括号和缩进。您应该阅读干净的代码,这本书比我解释得更好。但当我使用文件输出流保存照片时,图像位图旋转270度。有一个输入错误:ParcelFileDescriptor ParcelFileDescriptor=context.getContentResolver().openFileDescriptor(intent.getData()),“r”)在“getData()”后面有一个额外的括号谢谢你。我尝试了intent.data而不是intent.extras.get(“数据”)