Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ImageView不显示从Android Media/Gallery拍摄的图像_Android_Android Imageview_Android Gallery - Fatal编程技术网

ImageView不显示从Android Media/Gallery拍摄的图像

ImageView不显示从Android Media/Gallery拍摄的图像,android,android-imageview,android-gallery,Android,Android Imageview,Android Gallery,图像视图未显示库中的图像。大/小图像都不工作 private static final int RESULT_LOAD_IMAGE = 0; public void GalleryAction(View v){ Intent galleryintent=new Intent(); galleryintent.setType("image/*"); galleryintent.setAction("android.intent.action.GE

图像视图未显示库中的图像。大/小图像都不工作

private static final int RESULT_LOAD_IMAGE = 0;

public void GalleryAction(View v){

        Intent galleryintent=new Intent();
        galleryintent.setType("image/*");
        galleryintent.setAction("android.intent.action.GET_CONTENT");
        startActivityForResult(galleryintent, RESULT_LOAD_IMAGE);

    }

Uri resultFile;
int width;
int height;
Bitmap bm,bmtamp;
String pathString ;
Matrix matrixImage1;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        // TODO Auto-generated method stub
   if(requestCode==RESULT_LOAD_IMAGE){
       try{

            this.resultFile = data.getData();
            String[] arrayOfString = { MediaStore.Images.Media.DATA };
            Cursor localCursor = getContentResolver().query(SavingFrameActivity.outputFileUri, arrayOfString, null, null, null);

            localCursor.moveToFirst();
            int columnIndex = localCursor.getColumnIndex(arrayOfString[0]);
            this.pathString = localCursor.getString(columnIndex);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(getResources(), R.id.photoImage, options);
            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;
            String imageType = options.outMimeType;

           this.imgview=(ImageView)findViewById(R.id.photoImage);
           this.width=this.imgview.getWidth();
           this.height=this.imgview.getHeight();
this.bm=decodeSampledBitmapFromResource(getResources(), R.id.photoImage, 100, 100);
//this.bm = decodeSampledBitmapFromPath(this.pathString, this.imgview);
      this.imgview.setOnTouchListener(new MyonTouchListener());

      if (this.bm != null)
      {
        this.imgview.setImageBitmap(this.bm);
        this.matrixImage1 = new Matrix();
        this.imgview.setImageMatrix(this.matrixImage1);
      }
       }catch(Exception e){
           e.printStackTrace();
       }

   }


    }
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }


  public static int calculateInSampleSize(BitmapFactory.Options paramOptions, int paramInt1, int paramInt2)
    {
      int i = paramOptions.outHeight;
      int j = paramOptions.outWidth;
      int k = 1;
      int n;
      if ((i > paramInt2) || (j > paramInt1))
      {
        int m = Math.round(i / paramInt2);
        n = Math.round(j / paramInt1);
        if (m < n)
          k = m;
      }
      else
      {
        return k;
      }
      return n;
    }

试一下这个工作代码,我使用了相同的要求

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

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

SavingFrameActivity.java:108这行中的内容感谢@Anil,您的代码正在工作。但我使用了我在上面编写的代码,从其他类调用DecodeSampledBitMapFromResource。它现在正在工作。
 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

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