我正在创建一个用于浏览gallery中图像的android活动。但它不是从“Camera”文件夹中获取图像

时间:2015-05-27 13:04:17

标签: android android-activity android-camera android-gallery

我正在创建一个用于浏览图库中图像的Android活动。但它只拍摄其他文件夹中的图像,但不拍摄“相机”文件夹中的图像。

1 个答案:

答案 0 :(得分:0)

点击

按钮中的此代码
Yourbotton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    AlertDialog.Builder alertdialog1 =new AlertDialog.Builder(EditProfileActivity.this);
                    alertdialog1.setMessage("Please select a Profile image");               

                    alertdialog1.setNegativeButton("CAMERA", new DialogInterface.OnClickListener() {



                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            Intent cameraIntent = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                            startActivityForResult(cameraIntent,CAMERA_REQUEST);




            }
            });
            alertdialog1.setNeutralButton("GALLERY",new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {




                            Intent i = new Intent(Intent.ACTION_PICK,            
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        startActivityForResult(i, RESULT_LOAD_IMAGE);



                            }
                            });


                           alertdialog1.setPositiveButton("CLOSE",new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                     dialog.cancel();
                }
            });    


                    alertdialog1.show();
                }
            });     

这在onCreate()

之外运行
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
       if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
          Bitmap photo = (Bitmap) data.getExtras().get("data");

          edpprofilepic.setScaleType(ScaleType.FIT_XY);
          edpprofilepic.setImageBitmap(photo);
          imagepath =  ImageWrite(photo);


       }



       else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK 
           && null != data) {
           Uri selectedImage = data.getData();
           String[] filePathColumn = { MediaStore.Images.Media.DATA };
           Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, 
                           null, null, null);
           cursor.moveToFirst();
           int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
           String picturePath = cursor.getString(columnIndex);
           cursor.close();
          edpprofilepic.setScaleType(ScaleType.FIT_XY);
          edpprofilepic.setImageBitmap(BitmapFactory.decodeFile(picturePath));
          imagepath =  ImageWrite(BitmapFactory.decodeFile(picturePath));

        }
    }

      public String ImageWrite(Bitmap bitmap1)
     {

          String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
             OutputStream outStream = null;
             File file = new File(extStorageDirectory, "slectimage.PNG");

             try
             {

                 outStream = new FileOutputStream(file);
                 bitmap1.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                 outStream.flush();
                 outStream.close();



             }
             catch (FileNotFoundException e)
             {
                 e.printStackTrace();

             } catch (IOException e)
             {
                 e.printStackTrace();

             }
             String imageInSD = "/sdcard/slectimage.PNG";

             return imageInSD;

     }

我认为这对你有帮助