拍照不会将图片保存在图库中

时间:2013-06-17 19:47:14

标签: android android-alertdialog android-gallery android-photos

我有一个按钮,按下时我会收到一个alertDialog来选择一个选择:图库或拍照。 按下拍照并拍照时,图片未保存在图库中。为什么是这样? 我需要这张照片,因为后来我打算发一个帖子把它上传到我的服务器并将字符串添加到数据库。 有人能告诉我我做错了吗?

private void selectImage() {
        final CharSequence[] items = { "Take Photo", "Choose from gallery",
                "Cancel" };

        AlertDialog.Builder builder = new AlertDialog.Builder(AddBuilding.this);
        builder.setTitle("Add Photo!");

        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[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, REQUEST_CAMERA);
                }

                else if (items[item].equals("Choose from gallery")) {
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);
                }

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


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            File f = new File(Environment.getExternalStorageDirectory()
                    .toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bm;
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        btmapOptions);

                bm = Bitmap.createScaledBitmap(bm, 400, 300, true);
                btnImg.setImageBitmap(bm);
                btnImg.setScaleType(ScaleType.CENTER_INSIDE);

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

            String tempPath = getPath(selectedImageUri, AddBuilding.this);
            Bitmap bm;
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
            bm = Bitmap.createScaledBitmap(bm, 400, 300, true);
            btnImg.setImageBitmap(bm);
            btnImg.setScaleType(ScaleType.CENTER_INSIDE);
        }
    }
}



public String getPath(Uri uri, Activity activity) {
        String[] projection = { MediaColumns.DATA };
        Cursor cursor = activity
                .managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

2 个答案:

答案 0 :(得分:2)

您正在获得外部存储空间。而是保存您的图像:

MediaStore.Images.Media.insertImage(getContentResolver(), 
                                    Bitmap, 
                                    Title, 
                                    Description);

这会将图像保存在图库中。请参阅insertImage()

  

<强>参数
   cr 使用图像的内容解析器
   Path 要插入的图像的路径
   name 图像的名称
   description 图像描述

     

<强>返回
   URL 到新创建的图像

答案 1 :(得分:0)

它迟到了,但是给别人发了答案。 这样做

文件f =新文件(Environment.getExternalStorageDirectory()+“/ CameraImages / temp.jpg”);