Android 给出图像路径时,图像不会显示在imageview中
我正在尝试在ImageView上显示图像。我在Android 给出图像路径时,图像不会显示在imageview中,android,image,Android,Image,我正在尝试在ImageView上显示图像。我在/storage/sdcard/DCIM/Camera/SAMPLE IMAGES/xxx.png中有图像。我使用以下代码在ImageView上显示图像 public class MainActivity extends Activity { Button button,hi,addpic; final adapter info = new adapter(this); Runnable m_handlertask = null ; Str
/storage/sdcard/DCIM/Camera/SAMPLE IMAGES/xxx.png中有图像。我使用以下代码在ImageView上显示图像
public class MainActivity extends Activity
{ Button button,hi,addpic;
final adapter info = new adapter(this);
Runnable m_handlertask = null ;
String path,birth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
hi = (Button) findViewById(R.id.button3);
final adapter info = new adapter(this);
/* for(int i =1;i<=info.getrowcount();i++)
{
java.lang.String[] images_paths = {};
images_paths[i-1]=info.fetchsingles(i);
Toast.makeText(getApplicationContext(), images_paths[i-1], Toast.LENGTH_LONG).show();
}*/
hi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,newlist.class);
startActivity(i);
}
});
addpic = (Button) findViewById(R.id.button2);
addpic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,adpic.class);
startActivity(i);
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,padd.class);
startActivity(i);
}
});
Date date = new Date(0);
java.text.DateFormat dateFormat =
android.text.format.DateFormat.getDateFormat(getApplicationContext());
dateFormat.format(date);
final ImageView jpgView;
jpgView = (ImageView) findViewById(R.id.imageView1);
//adapter mDbAdapter;
// path = info.getpath(y);
path = info.getPath();
final Handler mHandler = new Handler();
m_handlertask = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
mHandler.postDelayed(m_handlertask,3000);
condition();
}
int i=3;
private void condition() {
// TODO Auto-generated method stub
if((i % 3 )== 0) //running 1 time
{
birthday();
i++;
}
else //running 2 times
{
images();
i++;
}
}
private void birthday() {
// TODO Auto-generated method stub
try
{
birth = info.getBirth();
Toast.makeText(getApplicationContext(), "This is b'day pic : "+birth, Toast.LENGTH_LONG).show();
//Drawable d = Drawable.createFromPath(birth);
//jpgView.setImageDrawable(d);
File sdCardPath = Environment.getExternalStorageDirectory();
Bitmap bitmap = BitmapFactory.decodeFile(sdCardPath+"/DCIM/Camera/SAMPLE IMAGES/"+path);
jpgView.setImageBitmap(bitmap);
// Bitmap bitmap = BitmapFactory.decodeFile(birth);
// jpgView.setImageBitmap(bitmap);
}
catch(NullPointerException er)
{
String ht=er.toString();
Toast.makeText(getApplicationContext(), ht, Toast.LENGTH_LONG).show();
}
}
private void images() {
// TODO Auto-generated method stub
try
{
path = info.getPath();
Toast.makeText(getApplicationContext(), "This is reg pic : "+path, Toast.LENGTH_LONG).show();
// Drawable d = Drawable.createFromPath(path);
// jpgView.setImageDrawable(d);
File sdCardPath = Environment.getExternalStorageDirectory();
Bitmap bitmap = BitmapFactory.decodeFile(sdCardPath+"/DCIM/Camera/SAMPLE IMAGES/"+path);
jpgView.setImageBitmap(bitmap);
// Bitmap bitmap = BitmapFactory.decodeFile(path);
// jpgView.setImageBitmap(bitmap);
}
catch(NullPointerException er)
{
String ht=er.toString();
Toast.makeText(getApplicationContext(), ht, Toast.LENGTH_LONG).show();
}
}
};
m_handlertask.run();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
公共类MainActivity扩展活动
{按钮按钮,hi,addpic;
最终适配器信息=新适配器(此);
可运行的m_handlertask=null;
字符串路径,出生;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.button1);
hi=(按钮)findViewById(R.id.button3);
最终适配器信息=新适配器(此);
/*对于(int i=1;i您在“/storage/sdcard/”
之间有一个空格。您应该使用er.printStackTrace()
打印堆栈跟踪以查看异常。此外,您不应该硬编码字符串路径;使用环境类以一致的方式引用文件位置。1)确保您已授予读取android清单文件中外部存储的权限
(二)
与其硬编码到外部存储器的路径,不如使用此选项
File sdCardPath = Environment.getExternalStorageDirectory();
然后添加文件夹和文件名
Bitmap bitmap = BitmapFactory.decodeFile(sdCardPath+"/DCIM/Camera/img_folder/xxx.png");
jpgView.setImageBitmap(bitmap);
您是否已授予在android清单中读取外部存储的权限?是的@GirishGowda,我已授予外部存储的读取和写入权限。抱歉@Emmanuel,该空间在问题中输入错误。但这不是问题。谢谢您的回答。我也使用了Environment类@Emmanuel。但我也得到了同样的问题。请查看问题中的更新代码。谢谢。谢谢@Girish Gowda的回答。我已经尝试了你的解决方案。但是,输出没有变化。除了位图之外,还有其他方式显示图像吗?我还有权读取和写入清单文件中的外部存储。那么可能是问题o如果你能提供完整的代码那就太好了。好的@Girish Gowda,我已经用完整的代码更新了我的问题。