2013-10-29 12 views
8

Ich habe eine Frage darüber, wie man ein Bild mit dem Kamera-Intent (oder der Kamera-API) aufnimmt und dann das Bild in ein imageView bringt, damit ich es in meiner Anwendung anzeigen kann. Das habe ich bisher.Foto mit Kameraabsicht aufnehmen und in imageView oder textView anzeigen?

ich ein Setup-Taste

Button btnPicture = (Button) findViewById(R.id.btn_picture); 
    btnPicture.setOnClickListener(this); 

ich ein Setup-Kamera Methode

private void Camera() { 
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, TAKE_PICTURE_CODE); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    startActivityForResult(intent, REQUEST_CODE); 
} 

Und das ist, wo ich bin verloren. Ich versuche das Bild, das ich aufgenommen habe, zu verarbeiten.

private void processImage(Intent intent) { 
    setContentView(R.layout.imagelayout); 
    ImageView imageView = (ImageView)findViewById(R.id.image_view); 
    cameraBitmap = (Bitmap)intent.getExtras().get("data"); 
    imageView.setImageBitmap(cameraBitmap); 
} 

Meine Absicht ist, das Bild anzuzeigen, das Sie in image_view aufgenommen haben. Ich erhalte keinen Fehler, nichts passiert. Wenn ich das Bild mache, werde ich gebeten, entweder ein weiteres Bild zu machen oder nachdem ich den Zurück-Knopf des Geräts benutzt habe, schließt sich die Anlegekraft. Es scheint, dass ich vollständig aus meiner Bewerbung herausgenommen wurde, und die Rückkehr ist ein großes Problem. Irgendwelche Vorschläge? Was vermisse ich?

O ja, und hier ist mein onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
     if(TAKE_PICTURE_CODE == requestCode) { 

     Bundle extras = data.getExtras(); 
     if (extras.containsKey("data")) { 
      Bitmap bmp = (Bitmap) extras.get("data"); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bmp.compress(Bitmap.CompressFormat.PNG, 100, baos); 
      byte[] image = baos.toByteArray(); 
      if (image != null) { 
       Log.d(TAG, "image != null"); 
      } 

     } else { 
      Toast.makeText(getBaseContext(), "Fail to capture image", Toast.LENGTH_LONG).show(); 
     } 

    } 
} 

Ich versuche, das Bild in getExtras zu setzen, und es dann in ein ByteArray zu speichern. War eine andere Sache, die ich versuchte zu tun. Nicht sicher, wie alles zusammen kommt.

+1

Geben Sie den Code für 'onActivityResult() '.. –

Antwort

14

Das Verfahren, das i um einfach und hilfreich ist dies:

MainActivity

private static String root = null; 
private static String imageFolderPath = null;   
private String imageName = null; 
private static Uri fileUri = null; 
private static final int CAMERA_IMAGE_REQUEST=1; 

public void captureImage(View view) { 

    ImageView imageView = (ImageView) findViewById(R.id.capturedImageview); 

      // fetching the root directory 
    root = Environment.getExternalStorageDirectory().toString() 
    + "/Your_Folder"; 

    // Creating folders for Image 
    imageFolderPath = root + "/saved_images"; 
    File imagesFolder = new File(imageFolderPath); 
    imagesFolder.mkdirs(); 

    // Generating file name 
    imageName = "test.png"; 

    // Creating image here 

    File image = new File(imageFolderPath, imageName); 

    fileUri = Uri.fromFile(image); 

    imageView.setTag(imageFolderPath + File.separator + imageName); 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    startActivityForResult(takePictureIntent, 
      CAMERA_IMAGE_REQUEST); 

} 

und dann in Ihrer Tätigkeit onActivityResult Methode:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK) { 

     switch (requestCode) { 
     case CAMERA_IMAGE_REQUEST: 

      Bitmap bitmap = null; 
      try { 
       GetImageThumbnail getImageThumbnail = new GetImageThumbnail(); 
       bitmap = getImageThumbnail.getThumbnail(fileUri, this); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      // Setting image image icon on the imageview 

      ImageView imageView = (ImageView) this 
        .findViewById(R.id.capturedImageview); 

      imageView.setImageBitmap(bitmap); 

      break; 

     default: 
      Toast.makeText(this, "Something went wrong...", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     } 

    } 
} 

GetImageThumbnail.java

public class GetImageThumbnail { 

private static int getPowerOfTwoForSampleRatio(double ratio) { 
    int k = Integer.highestOneBit((int) Math.floor(ratio)); 
    if (k == 0) 
     return 1; 
    else 
     return k; 
} 

public Bitmap getThumbnail(Uri uri, Context context) 
     throws FileNotFoundException, IOException { 
    InputStream input = context.getContentResolver().openInputStream(uri); 

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); 
    onlyBoundsOptions.inJustDecodeBounds = true; 
    onlyBoundsOptions.inDither = true;// optional 
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional 
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions); 
    input.close(); 
    if ((onlyBoundsOptions.outWidth == -1) 
      || (onlyBoundsOptions.outHeight == -1)) 
     return null; 

    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight 
      : onlyBoundsOptions.outWidth; 

    double ratio = (originalSize > 400) ? (originalSize/350) : 1.0; 

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio); 
    bitmapOptions.inDither = true;// optional 
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional 
    input = context.getContentResolver().openInputStream(uri); 
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); 
    input.close(); 
    return bitmap; 
} 
} 

und dann auf dem Image Onclick Methode wird wie folgt sein:

public void showFullImage(View view) { 
    String path = (String) view.getTag(); 

    if (path != null) { 

     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     Uri imgUri = Uri.parse("file://" + path); 
     intent.setDataAndType(imgUri, "image/*"); 
     startActivity(intent); 

    } 

} 
+0

Danke, dein Beispiel ist umfangreich, hat aber geholfen. Ich bin jedoch mit Gewalt konfrontiert. Wenn ich die onlyBoundsOptions.inJustDecodeBounds-Zeile loswerde, scheint das Problem zu beheben, aber die Dinge funktionieren immer noch nicht so, wie ich es möchte. Ich werde mich weiter einmischen. Aber du hast mir geholfen Fortschritte zu machen, danke! – portfoliobuilder

+0

@portfolioBuilder willkommen .. :) –

+0

Das funktioniert. Ziehen Sie in Betracht, getThumbnail() static zu machen, Sie benötigen keine Instanz seiner Klasse, um es zu verwenden. – lemuel

3

Foto aufzunehmen richtig Sie es in temporärer Datei gespeichert werden sollten, da die Daten in Folge Absicht null sein können:

final Intent pickIntent = new Intent(); 
pickIntent.setType("image/*"); 
pickIntent.setAction(Intent.ACTION_GET_CONTENT); 
final String pickTitle = activity.getString(R.string.choose_image); 
final Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); 
if (AvailabilityUtils.isExternalStorageReady()) { 
    final Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    final Uri fileUri = getCameraTempFileUri(activity, true); 
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
    new Intent[] { takePhotoIntent }); 
} 

activity.startActivityForResult(chooserIntent, REQUEST_CODE); 

Und dann Foto von Uri erhalten:

if (requestCode == ProfileDataView.REQUEST_CODE 
       && resultCode == Activity.RESULT_OK) { 
      final Uri dataUri = data == null ? getCameraTempFileUri(context, 
       false) : data.getData(); 
       final ParcelFileDescriptor pfd = context.getContentResolver() 
        .openFileDescriptor(imageUri, "r"); 
       final FileDescriptor fd = pfd.getFileDescriptor(); 
       final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd); 
     } 
+0

10x ein, aber Sie sollten auch' getCameraTempFileUri() 'posten. – WindRider

Verwandte Themen