我正在使用从Github获得的GestureImageView项目,可绘制文件夹中有几张图像:page1.jpg,page2.jpg,page3.jpg,......... page30.jpg。
我有一个名为pagenumber的变量,当我单击按钮时,该变量将增加,我也想将图像加载到GestureImageView中。这是我在主类中的代码:
pagenumber++;
GestureImageView view1 = (GestureImageView) findViewById(R.id.image);
String uriPath = "android.resource://"+getPackageName()+"/drawable/page"+String.valueOf(pagenumber);
Uri uri = Uri.parse(uriPath);
view1 .setImageURI(uri);
在GestureImageView.java中,代码为:
@Override
public void setImageURI(Uri mUri) {
if ("content".equals(mUri.getScheme())) {
try {
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = getContext().getContentResolver().query(mUri, orientationColumn, null, null, null);
if (cur != null && cur.moveToFirst()) {
imageOrientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
InputStream in = null;
try {
in = getContext().getContentResolver().openInputStream(mUri);
Bitmap bmp = BitmapFactory.decodeStream(in);
if(imageOrientation != 0) {
Matrix m = new Matrix();
m.postRotate(imageOrientation);
Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
bmp.recycle();
setImageDrawable(new BitmapDrawable(getResources(), rotated));
}
else {
setImageDrawable(new BitmapDrawable(getResources(), bmp));
}
}
finally {
if(in != null) {
in.close();
}
if(cur != null) {
cur.close();
}
}
}
catch (Exception e) {
Log.w("GestureImageView", "Unable to open content: " + mUri, e);
}
}
else {
setImageDrawable(Drawable.createFromPath(mUri.toString()));
}
if (drawable == null) {
Log.e("GestureImageView", "resolveUri failed on bad bitmap uri: " + mUri);
// Don't try again.
mUri = null;
}
}
好吧,我在GestureImageView中有一个空图像,它没有加载。日志猫说无法解码流:java.io.FileNotFoundException:/android.resource:/com.example.tests/drawable/page3:打开失败:ENOENT(没有这样的文件或目录)
有什么帮助吗?
最佳答案
退房:
https://github.com/jasonpolites/gesture-imageview/issues/21
还有:
GestureImageView goes blank when trying to set bitmap from camera programatically
其他人在stackoverflow中似乎也有同样的问题,然后在这里解决:D
(我为自己提出这个问题然后找到答案感到抱歉...)