从图库

时间:2016-04-12 15:30:02

标签: android

我不知道为什么它不适用于我的情况并且工作所有其他人

public class MainActivity extends Activity {
    private static final int imagess = 1;
    Button uploadimage, loadimage;
    ImageView selectimage, showimage;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        uploadimage = (Button) findViewById(R.id.uploadimage);
        loadimage = (Button) findViewById(R.id.loadimage);
        selectimage = (ImageView) findViewById(R.id.imageUpToLoad);
        showimage = (ImageView) findViewById(R.id.Imagedownload);


        selectimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryintent = new Intent(Intent.ACTION_PICK , MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryintent , imagess);
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == imagess && requestCode == RESULT_OK && data != null) {
            Uri imageuri = data.getData();
          selectimage.setImageURI(imageuri);

这是xml代码,也很完美。这访问我的画廊,但当我从画廊中选择图像时,我没有在图像视图中显示。不知道为什么它不起作用

<ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/imageUpToLoad"
        android:layout_gravity="center_horizontal"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="upload image"
        android:id="@+id/uploadimage"
        android:layout_below="@+id/selectimage"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/selectimage" />

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/Imagedownload"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load image"
        android:id="@+id/loadimage"
        android:layout_below="@+id/imageView2"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/imageView2" />

2 个答案:

答案 0 :(得分:0)

请修改onActivityResult

if (requestCode == imagess && requestCode == RESULT_OK && data != null) {
      Uri imageuri = data.getData();
      String[] projection = { MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(imageuri, projection, null, null, null);
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      String picturePath = cursor.getString(columnIndex);
      cursor.close();
      selectimage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}

答案 1 :(得分:0)

试试这个。将onActivityResult更改为此

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == imagess && requestCode == RESULT_OK && data != null) {
        Uri imageuri = data.getData();
        try 
        {
            Bitmap bitmapImage = decodeBitmap(imageuri);
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }
        selectimage.setImageBitmap(bitmapImage);
}

并添加此方法

public  Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

    final int REQUIRED_SIZE = 100;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}