2012-09-19 29 views
0

我试图从相机或图库中加载图像(不是URL)并将其保存到全局类。 (目前我正在尝试去看图像,但还没有定义类)。如何从图库加载图像到意图

所以我认为相机正确地返回图像,并将其放入包中,如果可能的话,我喜欢对图库使用相同的方法。

所以我必须:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode==RESULT_OK){ 
     Bundle extras = data.getExtras(); 
     bmp = (Bitmap) extras.get("data"); 

    } 
} 

而这两个选择,其中很明显,我做错了什么与画廊:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    // TODO Auto-generated method stub 
    switch(arg2){ 
    case 0: 
     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(i, cameraData); 
     break; 
    case 1: 

     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.setType("image/*"); 

     //i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(intent, 10); 
     break; 
    } 

我得到失败的结果传递:资源空指针异常: dat =内容://媒体/外部/图像/媒体/ 23

所以我想我做错了什么。

想法与在Instagram中看到的行为类似,拍照或选择现有的行为,选择时应将其存储在某个单一对象中,因为在再次显示图像之前可以选择3个选项我的应用程序

我不确定这是否是处理图像的最佳方式,因此这里的任何建议也是受欢迎的。

Tnx

回答

1

onActivityResult,试试这段代码。

InputStream in = getContentResolver().openInputStream(data.getData()); 
// get picture size. 
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeStream(in, null, options); 
in.close(); 
// resize the picture for memory. 
int width = options.outWidth/displayWidth + 1; 
int height = options.outHeight/displayHeight + 1; 
int sampleSize = Math.max(width, height); 
options.inSampleSize = sampleSize; 
options.inJustDecodeBounds = false; 
in = getContentResolver().openInputStream(data.getData()); 
// convert to bitmap with declared size. 
Bitmap bitmap = BitmapFactory.decodeStream(in, null, options); 
in.close(); 
+0

它帮助,即使我与三星手机有大问题,但工作在所有其他人。 – Balkyto

相关问题