如何显示点击图像的预览?

时间:2016-08-22 07:06:08

标签: android android-studio android-camera

使用自定义相机如何在单击相机并单击图像后显示全尺寸图像的预览,如果我们可以显示是否接受图像或丢弃图像,然后再将其保存到SD卡。(提示) :用于watsapp)

2 个答案:

答案 0 :(得分:0)

你将获得byte []中的图像,你可以将这个字节[]转换成位图,并可以在ImageView中显示它

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

答案 1 :(得分:0)

根据您的要求提供完整的代码如下。只需按照这个。这里u​​Image是Bitmap,imageView是你的图像显示的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;
        }
    }