Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

camera intent from Android website not working - Android

At this line: BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); it throws a fileNotFound exception. Here is the Logcat:

01-29 23:56:12.296: E/BitmapFactory(30046): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/Pictures/JPEG_20140129_235544_1090805596.jpg: open failed: ENOENT (No such file or directory)

That is within setPic(); But the file is saved and added to the gallery during starting the intent, so before onActivityResult, so it should be there. Do you see any issues? This code is taken from the Android devloper website http://developer.android.com/training/camera/photobasics.html

    static final int REQUEST_TAKE_PHOTO = 1001;

              private void dispatchTakePictureIntent() {
                  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  // Ensure that there's a camera activity to handle the intent
                  if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                      // Create the File where the photo should go
                      File photoFile = null;
                      try {
                          photoFile = createImageFile();
                      } catch (IOException ex) {
                          // Error occurred while creating the File
                      }
                      // Continue only if the File was successfully created
                      if (photoFile != null) {
                          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                  Uri.fromFile(photoFile));
                          startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                      }
                  }
              }

              private void galleryAddPic() {
                    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    File f = new File(mCurrentPhotoPath);
                    Uri contentUri = Uri.fromFile(f);
                    mediaScanIntent.setData(contentUri);
                    this.sendBroadcast(mediaScanIntent);
                }

EDIT this next code block is from onActivityResult:

else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == -1)){
                                     // Uri selectedImage = imageUri;
                                              mProfilePicPath = mCurrentPhotoPath;

                                              mPortraitPhoto = setPic();
                                              TextView tv = (TextView) findViewById(id.ProfilePicText);
                                tv.setText(mProfilePicPath);
                                          //}
                                     // }
                          }
                  }catch(Exception ex){
                          Log.d("shkdghrfb", ex.toString());
                  }
          }

              String mCurrentPhotoPath;

              private Bitmap setPic() {
                    // Get the dimensions of the View

                    // Get the dimensions of the bitmap
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                    int photoW = bmOptions.outWidth;
                    int photoH = bmOptions.outHeight;

                    // Determine how much to scale down the image
                    //int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

                    // Decode the image file into a Bitmap sized to fill the View
                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = 5;
                    bmOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                    //mImageView.setImageBitmap(bitmap);
                    return bitmap;
                }

              private File createImageFile() throws IOException {
                  // Create an image file name
                  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                  String imageFileName = "JPEG_" + timeStamp + "_";
                  File storageDir = Environment.getExternalStoragePublicDirectory(
                          Environment.DIRECTORY_PICTURES);
                  File image = File.createTempFile(
                      imageFileName,  /* prefix */
                      ".jpg",         /* suffix */
                      storageDir      /* directory */
                  );

                  // Save a file: path for use with ACTION_VIEW intents
                  mCurrentPhotoPath = "file:" + image.getAbsolutePath();
                  galleryAddPic();
                  return image;
              }

Manifest permissions I have added:

<uses-permission android:name="android.permission.CAMERA" android:required="false" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 211
user3164083 Avatar asked Jul 11 '26 12:07

user3164083


1 Answers

I thought having "file" in the filename was not a correct filename so deleted it. Changed mCurrentPhotoPath = "file:" + image.getAbsolutePath(); to mCurrentPhotoPath = image.getAbsolutePath();. If this is just a hack fix please let me know. It's important this works on all devices that are compatible.

like image 176
user3164083 Avatar answered Jul 14 '26 10:07

user3164083