ImageView仍为空白,Android

时间:2016-09-22 09:05:02

标签: java android xml android-studio bitmap

我允许用户选择图片并使用它在另一个imageview中设置为activity。但imageview仍为空白。以下是我的代码。

    private static final int REQUEST_CODE_GALLERY = 1;
    private static final int REQUEST_IMAGE_CAPTURE = 2;

    public ImageView CameraButton;
    public ImageView GalleryButton;
    public ImageView example;
    public Bitmap imageBitmap;
    public Bitmap bmp;

    ongallery object = new ongallery();

     @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     GalleryButton = (ImageView) findViewById(R.id.GalleryButton);
     CameraButton = (ImageView) findViewById(R.id.CameraButton);
    example = (ImageView) findViewById(R.id.example);
    GalleryButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                startActivityForResult(gallery, REQUEST_CODE_GALLERY);
                            }
                        });
      CameraButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                if (camera.resolveActivity(getPackageManager()) != null) {
                                    startActivityForResult(camera, REQUEST_IMAGE_CAPTURE);
                                }
                            }
                        });
                    }


     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
     if (requestCode == REQUEST_CODE_GALLERY  &&  resultCode == RESULT_OK  &&  null != data) {

     Uri chosen = data.getData();
    String[] filepath = { MediaStore.Images.Media.DATA };
     Cursor cursor = getContentResolver().query(chosen, filepath, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filepath[0]);
    String photoadd = cursor.getString(columnIndex);
    cursor.close();
    bmp = BitmapFactory.decodeFile(photoadd);

    try {
    //Write file
    String filename = "bitmap.png";
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    //Cleanup
    stream.close();
    bmp.recycle();
    //Pop intent
    Intent in1 = new Intent(this, ongallery.class);
    in1.putExtra("picture", filename);
    startActivity(in1);
    } catch (Exception e) {
    e.printStackTrace();
    }
     }
    else if (requestCode == REQUEST_IMAGE_CAPTURE  &&  resultCode == RESULT_OK  &&  null!=data) {
    Bitmap photo = (Bitmap) data.getExtras().get("data");
    try {
    //Write file
    String filename = "bitmap.png";
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
    photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
    //Cleanup
    stream.close();
    photo.recycle();
    //Pop intent
    Intent in1 = new Intent(this, ongallery.class);
     in1.putExtra("picture", filename);
    startActivity(in1);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }catch(Exception e){
    Toast.makeText(this,"Something went wrong",Toast.LENGTH_SHORT).show();
    }
    }
    }


//ongallery.java
    public class ongallery extends Activity {
    public  ImageView imgView;
    int xDim;
    int yDim;
    String filename;
     public  Bitmap finale = null ;
    public  Bitmap bmp = null;
    @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ongallery);
    imgView = (ImageView) findViewById(R.id.imgView);
    filename = getIntent().getStringExtra("picture");
    try {
    FileInputStream is = this.openFileInput(filename);
    bmp = BitmapFactory.decodeStream(is);
    is.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    imgView.setImageBitmap(decoder(filename,400,400));
    }
     @Override
    public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    xDim=imgView.getWidth();
    yDim=imgView.getHeight();
    }
    public Bitmap decoder(String filename, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    finale = BitmapFactory.decodeFile(filename, options);
    return finale;
    }

    int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int inSampleSize = 1;
    if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
    final int halfHeight = options.outHeight / 2;
    final int halfWidth = options.outWidth / 2;
     while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) >reqWidth) {
    inSampleSize *= 2;
     }
     }
    return inSampleSize;
    }
    }  

1 个答案:

答案 0 :(得分:0)

BitmapFactory.decodeFile()需要完整的路径名作为第一个参数。

public Bitmap decoder(String filename, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    String filepath = getFileStreamPath(filename).getPath();
    BitmapFactory.decodeFile(filepath, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    finale = BitmapFactory.decodeFile(filepath, options);
    return finale;
}