2017-05-18 3 views
0

Ich bin ein Bild erfassen und speichern Sie es im Speicher in mobilen, aber wenn ich dieses Bild bekomme kann es keine Sache in Image View. Ich habe viel Code ausprobiert, um Bilder von einer Datei zu bekommen, aber keiner von ihnen funktioniert in meinem Emulator oder einem echten Samsung-Gerät.Get Bild aus dem Speicher funktioniert nicht in Android

enter code here 

imageHolder = (ImageView)findViewById(R.id.captured_photo); 
    Button capturedImageButton = (Button)findViewById(R.id.photo_button); 
    capturedImageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(photoCaptureIntent, requestCode); 
     } 
    }); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(this.requestCode == requestCode && resultCode == RESULT_OK){ 
     Bitmap bitmap = (Bitmap)data.getExtras().get("data"); 

     String partFilename = currentDateFormat(); 
     storeCameraPhotoInSDCard(bitmap, partFilename); 


     // display the image from SD Card to ImageView Control 
     String storeFilename = "photo_" + partFilename + ".jpg"; 
     Bitmap mBitmap = getImageFileFromSDCard(storeFilename); 
     imageHolder.setImageBitmap(mBitmap); 

    } 
} 

    private String currentDateFormat(){ 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss"); 
    String currentTimeStamp = dateFormat.format(new Date()); 
    return currentTimeStamp; 
} 

private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){ 
    File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg"); 
    try { 
     FileOutputStream fileOutputStream = new FileOutputStream(outputFile); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); 
     fileOutputStream.flush(); 
     fileOutputStream.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
private Bitmap getImageFileFromSDCard(String filename){ 
    /* Bitmap bitmap = null; 
    File imageFile = new File(Environment.getExternalStorageDirectory() + filename); 
    try { 
     FileInputStream fis = new FileInputStream(imageFile); 
     bitmap = BitmapFactory.decodeStream(fis); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return bitmap; */ 

    File imageFile = new File(Environment.getExternalStorageDirectory() + filename); 
    // File imgFile = new File(filename); 
      //("/sdcard/Images/test_image.jpg"); 
    Bitmap myBitmap; 

    if(imageFile.exists()){ 

     myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 

     // ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 

     // myImage.setImageBitmap(myBitmap); 

     return myBitmap; 

    } 
    return null; 
} 
+0

http://stackoverflow.com/questions/30860646/upload-image-to-server-from-gallary-or-camera-android – jagapathi

Antwort

0

Zuerst stellen Sie sicher, dass Sie haben die „Zugriff External Storage“ und „Zugang Hardware Kamera“ Berechtigungen in „AndroidManifest“ deklariert und wenn Sie Android-Version 23 oder 23+ dann verwenden Sie die Berechtigungen auf nehmen Laufzeit. Wenn dies nicht das Problem ist, verwenden Sie diesen Code unten, es funktioniert gut für mich.

für Kamera:

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(takePicture, ACTION_REQUEST_CAMERA); 

Für Galerie:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType("image/*"); 

      Intent chooser = Intent.createChooser(intent, "Choose a Picture"); 
      startActivityForResult(chooser, ACTION_REQUEST_GALLERY); 

OnActivityResultMethod:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 

     switch (requestCode) { 
      case ACTION_REQUEST_GALLERY: 
       Uri galleryImageUri = data.getData(); 
       try{ 
        Log.e("Image Path Gallery" , getPath(getActivity() , galleryImageUri)); 
        selectedImagePath = getPath(getActivity() , galleryImageUri); 
       } catch (Exception ex){ 
        ex.printStackTrace(); 
        Log.e("Image Path Gallery" , galleryImageUri.getPath()); 
        selectedImagePath = galleryImageUri.getPath(); 
       } 

       break; 

      case ACTION_REQUEST_CAMERA: 
       // Uri cameraImageUri = initialURI; 
       Uri cameraImageUri = data.getData(); 
       Log.e("Image Path Camera" , getPath(cameraImageUri)); 
       selectedImagePath = getPath(cameraImageUri); 
       break; 
     } 

    } 
} 

Methode Weg von Bild zu erhalten von der Kamera zurück:

/** 
* helper to retrieve the path of an image URI 
*/ 
public String getPath(Uri uri) { 
    // just some safety built in 
    if(uri == null) { 
     // TODO perform some logging or show user feedback 
     return null; 
    } 
    // try to retrieve the image from the media store first 
    // this will only work for images selected from gallery 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null); 
    if(cursor != null){ 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    // this is our fallback here 
    return uri.getPath(); 
} 
Verwandte Themen