将图像加载到imageView中

时间:2013-11-27 10:59:01

标签: java android

我是一个新的Android开发人员我的第一个任务是Intent是从图库中选择一个图像并将其显示到imageview中,所以我做的是以下内容:

的xml:

 <Button
    android:id="@+id/Intent_btn"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_marginLeft="16dp"
    android:layout_toRightOf="@+id/button1"
    android:text="Intent button"
    android:onClick="openGallery" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignRight="@+id/Intent_btn"
    android:layout_centerVertical="true" />

代码:

ImageView imageview1;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     imageview1=(ImageView)findViewById(R.id.imageView1);
}

  public void openGallery(View v)
{
  Intent intent = new  Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 startActivityForResult(intent, RESULT_LOAD_IMAGE); 
}

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
        && null != data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = {MediaStore.Images.Media.DATA};

    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String filePath = cursor.getString(columnIndex);
    cursor.close();
    Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    imageview1.setImageBitmap(yourSelectedImage); 
}
}

我设法打开画廊并选择一张图片,但它从未加载会出现什么问题?

4 个答案:

答案 0 :(得分:1)

为清单文件添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

以下是有关如何执行此操作的示例代码:

protected void onActivityResult(int requestCode, int resultCode, 
       Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(
                               selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        }
    }
}

答案 2 :(得分:0)

添加此权限: -

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

使用它。这会奏效。将此代码放在onActivityResult()

Uri selectedImage = data.getData();
    String galleryImatePath = getRealPathFromURI(selectedImage);
    InputStream stream = getContentResolver().openInputStream(selectedImage);
    final Bitmap myImage = BitmapFactory.decodeStream(stream, null , bfOptions);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    myImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    imageview1.setImageBitmap(myImage);


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

答案 3 :(得分:0)

public void pickImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_CODE);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(
                    data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            image.setImageBitmap(bitmap);
            saveToInternalSorage(bitmap);
            // save image to internal memory
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

试试这样。