【发布时间】:2015-02-26 04:15:07
【问题描述】:
我可以在图库中加载手机随附的库存图片。 但是当我尝试从相机目录(在同一个画廊内)加载图像时,它没有显示出来。
但真正奇怪的是,它占用了空间,就好像图像加载成功一样。所以它占用空间,但它是不可见的。 (通过为 ImageView 提供仅在从相机目录加载图像后出现的黑色背景来确认这一点。)
这是我添加到 AndroidManifest.xml 的权限:
`<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />`
这是用于加载图像的代码:
public void loadImagefromGallery(View view)
{
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try
{
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data)
{
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgVieww);
Bitmap bitmapForImage = BitmapFactory.decodeFile(imgDecodableString);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(bitmapForImage);
}
else
{
Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
这是从教程中获取的。 如果您想自己尝试,它只有 1 个类和 1 个 xml 文件: http://programmerguru.com/android-tutorial/how-to-upload-image-to-php-server/
【问题讨论】: