2016-09-22 172 views
0

我允许用户选择图像并使用它在另一个activity中设置为imageview。但imageview保持空白。以下是我的代码。ImageView保持空白,Android

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; 
    } 
    } 

回答

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; 
} 
+0

嘿,谢谢那里,它真的为我工作。但是,仍然通过相机意图选择的图像在ImageView中显示时会降级。你有什么建议吗? –

+0

我调整了ImageView尺寸以包装内容,并发现如果使用相机意图拍照,缩略图太小。 –

+0

那么,根据[文档](https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE),您需要提供一个Uri,并将全尺寸的图像写入它。 – Iuliia