Use of bitmapped images in an application

0

As an experiment to develop a more advanced application, I was trying to make an application that consisted of a button and a imageView component, so that when I pressed the button a% bitmap that I have stored on my phone.

However after running my application, what happens is that when I press the button nothing appears, or rather, imageView appears a completely blank image. I think this might be related to the imageView method or even to the code structure for defining the image directory.

I can do this for the case where I use a JPEG image for example, and this image is stored in the folder setImageBitmap() of the package of my application. In this case I use drawable-mdpi to make the image display. But what I really wanted was to present an image that was stored somewhere on my mobile phone.

MainActivity.java file:

package com.example.showimages;

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.*; 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {


    File imgFile;
    ImageView myImage;
    Bitmap myBitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button) findViewById(R.id.button1); 
        String sd_path = Environment.getExternalStorageDirectory().toString();
        imgFile = new File(sd_path, "foto1.jpg");
        myImage = (ImageView) findViewById(R.id.imageView1); 
        myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        button1.setOnClickListener(new View.OnClickListener() { 
            @Override
            public void onClick(View arg0) { 
                myImage.setImageBitmap(myBitmap);
                } 
            }); 
        }
}

Activity_main.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.showimages.MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="126dp"
        android:layout_height="188dp"
        android:contentDescription="@android:string/VideoView_error_button"
        android:src="@drawable/abc_ic_clear" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Push" />

</LinearLayout>
    
asked by anonymous 29.10.2014 / 23:12

1 answer

1

Add the following permission in manifest.xml:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
30.10.2014 / 16:44