无法将库中的Bitmap加载到Canvas

时间:2016-04-20 22:52:58

标签: java android bitmap android-canvas

我正在尝试将我从图库中选取的图像加载到Canvas上,但由于某种原因它没有出现在它上面。 我正在使用THIS Canvas Library和方法在Canvas上绘制一个Bitmap this.canvas.drawBitmap(bitmap);,我正在使用它,但我不知道出了什么问题。

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)

1 个答案:

答案 0 :(得分:0)

我没有在manifest文件中设置适当的权限。这样做之后,一切正常。如果有人想知道如何做到这一点,请添加评论,我会更新答案。