Read JPEG from SD card and display in ImageView

All your problems with Audio, Video and Images.

Read JPEG from SD card and display in ImageView

Postby jbrohan » Thu Sep 24, 2009 8:49 pm

This is a project I've been struggling with and now it works. I'm publishing the TestImageLoad.java and main.xml

The file on the sd card is named in a constant, which is ok for my app. There are some options for the bm = BitmapFactory.decodeStream(is, null, null); function but I am ignoring them.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Image Load"
/>
<ImageView android:id="@+id/picview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

The TextView is non functional.

TestImageLoad.java
package com.example.testimageload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

public class TestImageLoad extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

File f = new File(Environment.getExternalStorageDirectory()+"/photo.jpg"); // or "/sdcard/photo.jpg"
FileInputStream is = null;
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
Log.d("John: ", "file Not Found");
}

Bitmap bm;
bm = BitmapFactory.decodeStream(is, null, null);
ImageView pic=(ImageView)this.findViewById(R.id.picview);
pic.setImageBitmap(bm);
}
}
jbrohan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Aug 28, 2009 11:12 am
Location: Montreal

Top

Postby azraeal » Tue Sep 29, 2009 8:36 pm

You're making it harder on yourself.

Use BitmapFactory.decodeFile(String path)
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 14 guests