2016-08-22 57 views
0

我写了一些代码拍摄照片并将其保存在外部存储器中。如果我运行该应用程序,并使图片,我得到文件未找到例外。未找到Android文件例外保存照片

这里是我的代码:

public class CameraActivity extends BaseActivity { 

    //variables for navigation drawer 
    private String[] navMenuTitles; 
    private TypedArray navMenuIcons; 

    //request code 
    private static final int ACTIVITY_START_CAMERA_APP = 1777; 

    //ImageView for the thumbnail 
    private ImageView mPhotoCapturedImageView; 

    //File for folder 
    private File folder; 

    //variable for timestamp 
    String timeStamp = ""; 

    //Requestcode for external Storage Permission 
    final int REQ_CODE_EXTERNAL_STORAGE_PERMISSION = 42;  

    //FloatAction Button to save the picture 
    private FloatingActionButton save; 

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

     /* 
     * Initialize nav draw items 
     */ 

     //load title from String.xml 
     navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

     //load icons from String.xml 
     navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons); 

     //set title and icons 
     set(navMenuTitles, navMenuIcons); 

     //initialize ImageView 
     mPhotoCapturedImageView = (ImageView) findViewById(R.id.imgViewThumbNail); 

     //creat new intent for the camera app 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 

    //creat and set a timestamp 
    timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 

    //check if permission granted 
     if (ActivityCompat.checkSelfPermission(CameraActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) 
       == PackageManager.PERMISSION_GRANTED) { 
      if (folder == null) { 
       createFolder(); 
      } 
     } else { 
      ActivityCompat.requestPermissions(CameraActivity.this, new String[] 
        {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_EXTERNAL_STORAGE_PERMISSION); 
     }  

    /* 
    //create a file with timestamp as title and save it in the folder 
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 
    File.separator + "MyIdea" + File.separator + "IdeaGallery" + File.separator + 
    "IMG_" + timeStamp + ".jpg"); */ 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(folder)); 
    //start the camera app 
    startActivityForResult(intent, ACTIVITY_START_CAMERA_APP); 
} 

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

     //Check that request code matches ours: 
     if (requestCode == ACTIVITY_START_CAMERA_APP) 
     { 
       File foto = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 
         "/Pictures/", "MyIdea/IdeaGalery/" + 
         "IMG_" + timeStamp + ".jpg"); 
       Bitmap bitmap = decodeSampledBitmapFromFile(foto.getPath(), 1000, 700); 
       mPhotoCapturedImageView.setImageBitmap(bitmap); 
       MediaScannerConnection.scanFile(CameraActivity.this, new String[]{foto.getPath()}, new String[]{"image/jpeg"}, null); 

      //Get our saved file into a bitmap object: 
      //File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator + "MyIdea" + File.separator + "IdeaGallery" + File.separator + "IMG_" + timeStamp + ".jpg"); 
      //Bitmap photoCapturedBitmap = BitmapFactory.decodeFile(mImageFileLocation); 
     } 
    } 

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
    { // BEST QUALITY MATCH 

     //First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     // Calculate inSampleSize, Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     options.inPreferredConfig = Bitmap.Config.RGB_565; 
     int inSampleSize = 1; 

     if (height > reqHeight) 
     { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } 
     int expectedWidth = width/inSampleSize; 

     if (expectedWidth > reqWidth) 
     { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 

     options.inSampleSize = inSampleSize; 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false;  
     return BitmapFactory.decodeFile(path, options); 
    } 

    private void createFolder() { 

     folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/", "MyIdea/IdeaGalery/" + 
      "IMG_" + timeStamp + ".jpg"); 
     folder.mkdir(); 
     Toast.makeText(getApplicationContext(), "Folder created", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     if (requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION && grantResults.length > 0 && 
       grantResults [0] == PackageManager.PERMISSION_GRANTED) { 
      createFolder(); 
     } 
    } 
} 

在Android清单权限设定ALS

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

有人可以帮助请帮我找到错误?

+0

你为什么要使用该文件的路径之间的逗号? – Chordin4tion

+0

好问题:D。我在 '文件夹=新的文件固定它(Environment.getExternalStorageDirectory()getAbsolutePath()+ “/图片/ MyIdea/IdeaGalery /” + “IMG_” +的timeStamp + “.JPG”);' 但文件未找到异常仍然存在。 – dudi

+0

请发布完整的消息..并告诉哪个语句导致该消息。 – greenapps

回答

0

我清理了我的代码,我希望你能更好地理解它。首先我检查,如果文件夹存在,如果没有,我创建它。然后我为图像创建一个文件。然后,我从图像中创建一个Uri,并将其设置为将ExpEx方法放置在意图上。

在活动结果上,我从数据中读取uri并将其包装为一个字符串。

这是我的新代码:

//check if imageFolder already exist 
    if (imageFolder == null) { 
     //create folder if don't exist 
     imageFolder = new File(Environment.getExternalStorageDirectory(), "MyIdea/IdeaGallery/"); 
     imageFolder.mkdir(); 
    } 

    //create imageFile 
    image = new File(imageFolder, "IMG_" + timeStamp + ".jpg"); 

    //get the uri of the imageFile 
    Uri uriSavedImage = Uri.fromFile(image); 

    //put uri path of the image to the intent 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    //start the camera app 
    startActivityForResult(intent, ACTIVITY_START_CAMERA_APP); 
} 

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

    //Check that request code matches ours: 
    if (requestCode == ACTIVITY_START_CAMERA_APP) 
    { 
     if (resultCode == RESULT_OK) { 
      //Get our saved file into a bitmap object: 
      Bitmap bitmap = decodeSampledBitmapFromFile(String.valueOf(data.getData()), 1000, 700); 
      mPhotoCapturedImageView.setImageBitmap(bitmap); 
      MediaScannerConnection.scanFile(CameraActivity.this, new String[]{image.getPath()}, 
        new String[]{"image/jpeg"}, null); 
     } 
    } 
} 

如果我看一下Android的监视器,我不能找到FileNoTFound例外。但是,如果我检查我的设备,我找不到任何文件夹或图像。 此消息IST表示在Android监视器:

12月8日至22日:41:08.302 17152-17196/com.example.dudi.myidea I /肾上腺EGL:QUALCOMM生成:15年10月21日,369a2ea,I96aee987eb 08-22 12:41:08.303 17152-17196/com.example.dudi.myidea I/OpenGLRenderer:初始化的EGL,版本1.4 08-22 12:41:08.827 17152-17189/com.example。 dudi.myidea W/Settings:设置airplane_mode_on已从android.provider.Settings.System移至android.provider.Settings.Global,并返回只读值。 08-22 12:41:08.882 17152-17152/com.example.dudi.myidea I /编舞:跳过37帧!应用程序可能在其主线程上做了太多工作。 12月8日至22日:41:08.906 17152-17196/com.example.dudi.myidea V /的renderScript:0xa00bc000下水螺纹(或多个),CPU的4

+0

好吧,我发现resultCode上的else语句,resultCode返回0。 – dudi

0

Finaly我看着Android Developer Tutorial和改变了我的代码,如示例在教程中。现在它工作正常。在这里我的工作代码:

public class CameraActivity extends BaseActivity { 

//variables for navigation drawer 
private String[] navMenuTitles; 
private TypedArray navMenuIcons; 

//request code 
private static final int ACTIVITY_START_CAMERA_APP = 1777; 

//ImageView for the thumbnail 
private ImageView mPhotoCapturedImageView; 

//variable for timestamp 
//String timeStamp = ""; 

private static final int MEDIA_TYPE_IMAGE = 1; 
private Uri fileUri; 

//File for folder 
//private File imageFolder; 

//File for image; 
//private File image; 


//FloatAction Button to save the picture 
private FloatingActionButton save; 

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


    /* 
    * Initialize nav draw items 
    */ 

    //load title from String.xml 
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

    //load icons from String.xml 
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons); 

    //set title and icons 
    set(navMenuTitles, navMenuIcons); 

    //initialize ImageView 
    mPhotoCapturedImageView = (ImageView) findViewById(R.id.imgViewThumbNail); 

    //initialize Camera Intent 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    //set file uri 
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

    //put Extra for onActivityResult 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    //start camera app 
    startActivityForResult(intent, ACTIVITY_START_CAMERA_APP); 

} 

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

    //Check that request code matches ours 
    if (requestCode == ACTIVITY_START_CAMERA_APP) 
    { 
     if (resultCode == RESULT_OK) { 
      //Get our saved file into a bitmap object: 
      Bitmap bitmap = decodeSampledBitmapFromFile(fileUri.getPath(), 1000, 700); 
      mPhotoCapturedImageView.setImageBitmap(bitmap); 
      MediaScannerConnection.scanFile(CameraActivity.this, new String[]{fileUri.getPath()}, 
        new String[]{"image/jpeg"}, null); 
     } else if (requestCode == RESULT_CANCELED) { 
      Toast.makeText(this, "Error, user cancelled the image capture", Toast.LENGTH_LONG).show(); 
     } else { 
      Toast.makeText(this, "Imagecapture failed.", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

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

/* Create a file to save an image */ 
private static File getOutputMediaFile(int type) { 

    //check if SD-Card is mounted 
    Boolean isMounted = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 

    File mediaStorageDir = null; 

    if (isMounted) { 
     //create storage directory if does not exist 
     mediaStorageDir = new File (Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "IdeaGallery"); 

     if (! mediaStorageDir.exists()) { 
      if (! mediaStorageDir.mkdir()) { 
       Log.d("IdeaGallery", "failed to create"); 
       return null; 
      } 
     } 

    } else { 
     Log.d("IdeaGallery", "failed to create"); 
    } 

    //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; 
} 

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH 

    //First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize, Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    options.inPreferredConfig = Bitmap.Config.RGB_565; 
    int inSampleSize = 1; 

    if (height > reqHeight) 
    { 
     inSampleSize = Math.round((float)height/(float)reqHeight); 
    } 
    int expectedWidth = width/inSampleSize; 

    if (expectedWidth > reqWidth) 
    { 
     inSampleSize = Math.round((float)width/(float)reqWidth); 
    } 

    options.inSampleSize = inSampleSize; 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 

    return BitmapFactory.decodeFile(path, options); 
} 

}