我试图将我从画廊中挑选的图像加载到Canvas上,但是由于某种原因,它没有显示在画布上。
我正在使用THIS Canvas Library,并且在上将位图绘制到画布上的方法正在使用,但是我不知道出了什么问题。

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>


draw_activity.xml:

<?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>


请注意,我没有尝试将位图正确地放置在画布上,因为在API文档中没有提到这种类型的东西。我用来从图库中加载图像的方法可能并不完全正确,如果是这样,请告诉我我做错了什么,应该怎么做。一个解释将不胜感激!

日志:

 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)

最佳答案

我没有在manifest文件中设置适当的权限。之后,一切正常。如果有人想知道该怎么做,请添加评论,我将更新答案。

关于java - 无法将位图从图库加载到 Canvas ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36756621/

10-08 23:51