簡體   English   中英

如何從圖庫加載圖像到意圖

[英]How to load image from gallery to intent

我正在嘗試從相機或圖庫加載圖像(而不是URL)並將其保存到全局類。 (目前,我正嘗試獲取圖像,尚未定義任何類)。

因此,我認為相機可以正確返回圖像,並將其放入包中,如果可能的話,我喜歡對Gallery使用相同的方法。

所以我有:

@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 = content:// media / external / images / media / 23

所以我想我做錯了。

想法類似於在Instagram上看到的行為,拍照或選擇現有的想法,並且應該將其存儲在某個單調對象中,因為在我的應用中再次顯示圖像之前,我將有3個選項可供選擇。

我不確定這是否是處理圖像的最佳方法,因此也歡迎此處提出任何建議。

特納克斯

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM