Use of bitmap images in an application

Asked

Viewed 125 times

0

As a trial for the development of a more advanced application, I was trying to make an application that consisted of a button and a component imageView, in such a way that when the imageView a type image bitmap I have stored in my phone.

However after running my application, what happens is that when I press the button nothing appears, or better, in the imageView a blank image appears. I think this may be related to the method setImageBitmap() or even with the code structure for defining the direction of this image.

I can do this in case I use a JPEG image for example, and this image is stored in the folder drawable-mdpi package of my application. In this case I use the setImageResource() to display the image. However, what I really wanted was to display an image that was stored somewhere on my 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>
  • Thanks! I’ve got it! The problem was related to the permissions, because I had not put any... I added this permission and it already worked correctly! <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

1 answer

1


Add the following permission in the manifest.xml:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.