android如何读取sd卡的图片并显示
答案:1 悬赏:0 手机版
解决时间 2021-03-01 06:07
- 提问者网友:酱爆肉
- 2021-02-28 05:47
android如何读取sd卡的图片并显示
最佳答案
- 五星知识达人网友:北方的南先生
- 2021-02-28 05:52
首先你要在AndroidManifest.xml申请读取sdcard的权限
关键代码:
package com.sdcardread;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
private TextView textView1;
private LinearLayout linearLayout1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
boolean isSdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);// 判断sdcard是否存在
if (isSdCardExist) {
String sdpath = Environment.getExternalStorageDirectory()
.getAbsolutePath();// 获取sdcard的根路径
textView1.setText("sd卡是存在的。以下是sdcard下的img25.jpg!");
String filepath = sdpath + File.separator + "img25.jpg";
File file = new File(filepath);
ImageView imageView = new ImageView(this);//创建一个imageView对象
if (file.exists()) {
Bitmap bm = BitmapFactory.decodeFile(filepath);
// 将图片显示到ImageView中
imageView.setImageBitmap(bm);
linearLayout1.addView(imageView);
}
} else {
textView1.setText("sd卡不存在!");
}
}
}
package com.sdcardread;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
private TextView textView1;
private LinearLayout linearLayout1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
boolean isSdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);// 判断sdcard是否存在
if (isSdCardExist) {
String sdpath = Environment.getExternalStorageDirectory()
.getAbsolutePath();// 获取sdcard的根路径
textView1.setText("sd卡是存在的。以下是sdcard下的img25.jpg!");
String filepath = sdpath + File.separator + "img25.jpg";
File file = new File(filepath);
ImageView imageView = new ImageView(this);//创建一个imageView对象
if (file.exists()) {
Bitmap bm = BitmapFactory.decodeFile(filepath);
// 将图片显示到ImageView中
imageView.setImageBitmap(bm);
linearLayout1.addView(imageView);
}
} else {
textView1.setText("sd卡不存在!");
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯