2016-08-22 12 views
0

カスタムカメラを使用する方法クリックしたカメラから画像をクリックした後にプレビューを表示するにはどうすればいいですか、画像を受け入れるかどうかを示すことができる場合はプレビューでプレビューできます(ヒント: watsappで使用されるように)クリックした画像のプレビューを表示する方法は?

+0

私はプログラムで次のようにすることをお勧めします。キャプチャイメージ - > tempディレクトリをsdカード(whtsappなど)に作成して保存します。圧縮してください。そのパスから画像を取得して表示します。あなたは私が言及した各ステップのためにスタックのオーバーフローでGoogleにすることができます。また、ビットマップが適切に処理されない場合は、OOM例外を遭遇することもあります。 – rmdroid

+0

コードを使ってその例を教えてください。 – radhika

+0

私はあなたを解決策に導きます。下のリンクをクリックしてください。カメラAPIはうまく説明されています。 http://www.androidhive.info/2013/09/android-working-with-camera-api/ – rmdroid

答えて

0

あなたは[]バイトで画像を取得します、あなたはビットマップに[]このバイトを変換することができますし、あなたの条件に応じて、完全なコードは以下のとおりであるImageViewの

Bitmap bitmap = BitmapFactory.decodeByteArray(yourbytearray, 0, yourbytearray.length); 
0

でそれを表示することができます。ちょうどthis.UmageはBitmap、imageViewは画像が表示される画像ビューです。

private void selectImage() { 

     final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(AddownRecipeFromHome.this); 
     builder.setTitle("Add Photo!"); 
     builder.setItems(options, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       if (options[item].equals("Take Photo")) 
       { 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg"); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
        startActivityForResult(intent, 1); 
       } 
       else if (options[item].equals("Choose from Gallery")) 
       { 
        Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(intent, 2); 

       } 
       else if (options[item].equals("Cancel")) { 
        dialog.dismiss(); 
       } 
      } 
     }); 
     builder.show(); 
    } 


    private Uri getTempUri() { 
     return Uri.fromFile(getTempFile()); 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      if (requestCode == 1) { 
       File f = new File(Environment.getExternalStorageDirectory().toString()); 
       for (File temp : f.listFiles()) { 
        if (temp.getName().equals("temp.jpg")) { 
         f = temp; 
         break; 
        } 
       } 
       try { 

        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

        uImage = BitmapFactory.decodeFile(f.getAbsolutePath(), 
          bitmapOptions); 

        imageView.setImageBitmap(uImage); 

        String path = android.os.Environment 
          .getExternalStorageDirectory() 
          + File.separator 
          + "Phoenix" + File.separator + "default"; 
        f.delete(); 
        OutputStream outFile = null; 
        File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); 
        try { 
         outFile = new FileOutputStream(file); 
         uImage.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 
         outFile.flush(); 
         outFile.close(); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } else if (requestCode == 2) { 

       Uri selectedImage = data.getData(); 
       String[] filePath = { MediaStore.Images.Media.DATA }; 
       Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); 
       c.moveToFirst(); 
       int columnIndex = c.getColumnIndex(filePath[0]); 
       String picturePath = c.getString(columnIndex); 
       c.close(); 
       uImage = (BitmapFactory.decodeFile(picturePath)); 
       Log.e("path of imag", ""+picturePath); 
       imageView.setImageBitmap(uImage); 
      } 
     else if (requestCode == 3) { 
       try { 
        Log.e("testing", "return data is " + data.getData()); 

        String filePath = Environment.getExternalStorageDirectory() 
          + "/" + TEMP_PHOTO_FILE; 
        System.out.println("path " + filePath); 
        uImage = BitmapFactory.decodeFile(filePath); 
        ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
        uImage.compress(Bitmap.CompressFormat.PNG, 100, bao); 
        ba = bao.toByteArray(); 
        imageView.setImageBitmap(uImage); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
} 


    private File getTempFile() { 

     if (Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED)) { 

      File file = new File(Environment.getExternalStorageDirectory(), 
        TEMP_PHOTO_FILE); 
      try { 
       file.createNewFile(); 
      } catch (IOException e) { 
      } 

      return file; 
     } else { 

      return null; 
     } 
    } 
関連する問題