【问题标题】:Imageview from phone galley来自电话厨房的图像视图
【发布时间】:2012-03-19 20:57:47
【问题描述】:

我试图从我的手机厨房访问并选择一个图像到我的应用程序 imageView,使用 android。到目前为止,我设法设置到厨房并选择我想要使用的图像,唯一的问题是一旦我选择了我想要的图像,我就无法将它放到 imageview 上。问题似乎是我将 imageview 类与我的主类分开。我检查过,找不到解决方案。

这是 mediagalley 类的代码

公共类 MediaGallery 扩展 Activity { public static final int SELECT_IMAGE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}

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

if (resultCode == RESULT_OK && requestCode == SELECT_IMAGE) {
        Uri selectedImage = data.getData();
        String path = getPath(selectedImage);

        Bitmap bitmapImage = BitmapFactory.decodeFile(path);
        ImageView image = (ImageView) findViewById(R.id.imageView1);
        image.setImageBitmap(bitmapImage);


    }
}

public String getPath(Uri uri) {
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(uri, filePathColumn, null,null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

    return cursor.getString(columnIndex);
}

}

及主类代码

case R.id.gallery_button:
            MediaGallery activ1 = new MediaGallery();
            Intent gallery = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, 1);
            activ1.onActivityResult(1, 1, gallery);
            break;

您给予的任何帮助都会非常感激

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    您已声明 MediaGallery 是另一个 Activity。您是否尝试在您选择按钮以启动选择器的同一活动上或在新活动中显示 ImageView?如果是前者,请将 onActivityResult 代码放入您的主类中。如果是后者,那么您的主类应该将 Intent 数据传递给 MediaGallery 类。

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 2017-08-06
      • 2016-07-17
      • 2013-06-01
      • 2019-04-29
      • 1970-01-01
      相关资源
      最近更新 更多