Gallery Intent无法在Android 4.0.3上运行

时间:2013-02-12 14:15:13

标签: android android-layout android-intent android-gallery

这是我从“图库”中获取图像的代码

intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, GALLERY_INTENT);

这是onActivityResult()方法

if(requestCode==2 && resultCode == RESULT_OK ){

            Uri _uri = data.getData();
            if (_uri != null) {

            //User had pick an image.
            Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
            cursor.moveToFirst();

            //Link to the image
            final String imageFilePath = cursor.getString(0);
            cursor.close();

            Intent intent = new Intent(HomeActivity.this,ConfirmPicture.class);
            intent.putExtra(INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED, true);
            intent.putExtra("IMAGE_PATH", imageFilePath);
            intent.putExtra("OUTLET_ID", 0);
            intent.putExtra("OUTLET_NAME", "name");
            startActivity(intent);
           }

此代码在android 2.3.4版本上运行良好,但是当我在4.0.3设备上测试此代码时,它无法正常工作,可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
            ZootOutObject.objectUri = getImagePath();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, ZootOutObject.objectUri);
            startActivityForResult(intent, CAMERA_INTENT);

if(requestCode == 1&& resultCode == RESULT_OK){

             String pathName=Environment.getExternalStorageDirectory()+"/folder_name/image_zootout.jpg";

             Intent intent = new Intent(getApplicationContext(),PhotoEditorActivity.class);
             intent.putExtra("IMAGE_PATH", pathName);
             intent.putExtra("OUTLET_ID", 0);
             intent.putExtra("OUTLET_NAME", "");
             startActivity(intent);

       }


private Uri getImagePath() {
    // If sd card is available
        if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
            String path = Environment.getExternalStorageDirectory() .getAbsolutePath();
            filename = "image_zootout.jpg";
            path += "/folder_name";
            File file = new File(path);
            if (!file.isDirectory()) {
                file.mkdirs();
            }
            path += "/" + filename;
            File imageFile = new File(path);
            if (!imageFile.exists()) {
                try {
                    imageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
            return Uri.fromFile(imageFile);
        }else{ // If sd card is not available
            return null;
        }
    }