无法将图像保存到存储 - Android

时间:2017-02-27 23:31:42

标签: java android

package ar.camera;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE;

public class MainActivity extends AppCompatActivity {

    static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
    private Uri fileUri;

    //create the file URI
    private static Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }


    private static File getOutputMediaFile(int type){

        final File mediaStorageDir;

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "EMFDetectingApp");
        } else{
            mediaStorageDir = new File ("/storage/sdcard0/EMFDetectingApp");
        }

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

        //create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile = null;
        if(type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
            return mediaFile;
        } else{
            return null;
        }



    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        //create the uri of a file to save the image
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        //specifying the path and file name for teh received image
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE){
            if(resultCode == RESULT_OK){
                //image cpatured and saved to fileUri specifies in the Intent
                Toast.makeText(this, "Image successfully saved", Toast.LENGTH_SHORT).show();
            }
            else if(resultCode == RESULT_CANCELED){
                Toast.makeText(this, "Image capture cancelled", Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(this, "Image capture failed", Toast.LENGTH_SHORT).show();

            }
        }
    }


}

我创建了一个应用程序,它使用相机拍摄并拍摄然后保存。但是,当我运行上面的代码时,我可以拍照,但图像不会被保存。我无法在画廊或SD卡中找到它。任何人都可以在这段代码中发现错误吗?

1 个答案:

答案 0 :(得分:1)

  

有人能在这段代码中发现错误吗?

  • 您在设置startActivityForResult()之前致电Intent - 在致电putExtra()之前致电startActivityForResult()

  • 从不硬编码路径(例如/storage/sdcard0/EMFDetectingApp),因为所有设备上可能不存在此类路径。

  • 如果是removable storage,则无法在Android 4.4及以上设备上写信/storage/sdcard0/

  • 如果Uri.fromFile()为24或更高,则无法在Android 7.0及更高版本的设备上使用targetSdkVersion

  • 在您have MediaScannerConnection index the photo之前,您的照片将无法在Windows / macOS / Linux或大多数图库应用中看到。