Java 无法将位图从库加载到画布
我正在尝试将从图库中挑选的图像加载到画布上,但由于某些原因,它没有显示在画布上。 我正在使用和在画布上绘制位图的方法有Java 无法将位图从库加载到画布,java,android,bitmap,android-canvas,Java,Android,Bitmap,Android Canvas,我正在尝试将从图库中挑选的图像加载到画布上,但由于某些原因,它没有显示在画布上。 我正在使用和在画布上绘制位图的方法有this.Canvas.drawBitmap(位图)我正在使用它,但我不知道出了什么问题 MainActivity.java: public class MainActivity extends AppCompatActivity { private CanvasView canvas; private DrawActivity drawActivity;
this.Canvas.drawBitmap(位图)代码>我正在使用它,但我不知道出了什么问题
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private CanvasView canvas;
private DrawActivity drawActivity;
//Variables for Selecting Photo from gallery to load onto canvas
private static final int SELECT_PHOTO = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
canvas = (CanvasView) findViewById(R.id.canvas);
}
public void clearCanvas(View v){
this.canvas.clear();
}
public void loadImage(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
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);
this.canvas.drawBitmap(yourSelectedImage);
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="tech.arvisapps.thumbnailmaker.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:alpha="0.5">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/draw_activity" />
</android.support.design.widget.CoordinatorLayout>
我没有在清单
文件中设置适当的权限。完成后,一切正常。如果有人想知道怎么做,请添加评论,我将更新答案。我没有在清单
文件中设置适当的权限。完成后,一切正常。如果有人想知道如何做到这一点,请添加评论,我将更新答案。post logcat。。。。。。。。。。。!!日志后。。。。。。。。。。。!!
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DDDDDD"
android:layout_gravity="center"
android:orientation="horizontal" >
<com.android.graphics.CanvasView
android:id="@+id/canvas"
android:layout_width="280dp"
android:layout_height="280dp"
android:layout_gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clearCanvas"
android:onClick="clearCanvas"
android:layout_gravity="bottom|center"
android:text="CLEAR"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loadImage"
android:onClick="loadImage"
android:layout_gravity="bottom|left"
android:text="LOAD"/>
</FrameLayout>
04-23 14:40:28.698 19671-19671/tech.arvisapps.thumbnailmaker E/BitmapFactory:
Unable to decode stream: java.io.FileNotFoundException:
/storage/emulated/0/Thumbnail Maker/Image-5322.jpg: open failed: EACCES (Permission denied)