我正在尝试在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 ;
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;
} }
我已经阅读了其他问题和教程,但我找到了相同的显示方法,就像我在上面的代码中所做的那样。此处不显示图像。我没有在logcat中找到任何错误消息。请建议我,代码中的任何改进。
提前致谢。
答案 0 :(得分:0)
"/storage/ sdcard/"
之间有空格。您应该打印堆栈跟踪以使用Exception
查看er.printStackTrace()
。此外,您不应该硬编码字符串路径;使用Environment
Class
以一致的方式引用文件位置。
答案 1 :(得分:0)
1)确保您已授权在Android清单文件中读取外部存储空间。
2) 而不是硬编码外部存储的路径使用它。
File sdCardPath = Environment.getExternalStorageDirectory();
然后添加您的文件夹和文件名..
Bitmap bitmap = BitmapFactory.decodeFile(sdCardPath+"/DCIM/Camera/img_folder/xxx.png");
jpgView.setImageBitmap(bitmap);