2016-07-23 8 views
0

Ich bin Anfänger in Android Programmierung und ich möchte nur das aufgenommene Bild durch Kamera angezeigt werden und hier ist mein CodeBitmap kehrt Null in Android

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 = getExternalFilesDir(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(); 
    return image; 
} 

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) { 
      fileUri = FileProvider.getUriForFile(this, 
        "com.example.android.fileprovider", 
        photoFile); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
    } 
} 
private void previewSavedImage() { 
    try { 
     // hide video preview 
     videoPreview.setVisibility(View.GONE); 

     imgPreview.setVisibility(View.VISIBLE); 

     // bimatp factory 
     BitmapFactory.Options options = new BitmapFactory.Options(); 

     // downsizing image as it throws OutOfMemory Exception for larger 
     // images 
     options.inSampleSize = 2; 

     String path = fileUri.getPath(); 

     //File img_file = new File(getFilesDir(), path); 



     final Bitmap bitmap = BitmapFactory.decodeFile(path, options); 

     imgPreview.setImageBitmap(bitmap); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } 
} 

In obigem Code Bitmap immer keine Rückkehr und wegen dieser I kann kein Bild anzeigen. Mache ich etwas?

+0

Haben Sie eine Fehlermeldung erhalten? –

+0

Nein, aber "Bitmap" -Wert ist null. – Yogesh

+0

Ich habe diesen Code ausprobiert, aber OutOfMemoryException wird nicht ausgegeben. Ich mache mir nur Sorgen, ob Android das aufgenommene Bild an einem bestimmten Ort speichert. Wie kann ich das überprüfen? – Yogesh

Antwort

1

Endlich habe ich das gelöst. Das Problem ist mit "file:" string in mCurrentPhotoPath, ich muss es nur entfernen, bevor Sie es in der Methode decodeFile verwenden.

Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath.replace("file:", "")); 
+0

Warum hast du es dann dort hingelegt? Und wenn Sie solche benötigen, dann sollten Sie "file: //" verwendet haben. – greenapps

+0

Guter Fang! Das ist eine Liebe zum Detail. –

+0

Ich werde das entfernen. Ich habe nur die Dokumentation verfolgt und dachte, dass es später nützlich sein könnte. – Yogesh

Verwandte Themen