0

我正在尝试在 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 中没有发现任何错误消息。请建议我,对代码进行任何改进。

提前致谢。

4

2 回答 2

0

之间有空格"/storage/ sdcard/"。您应该打印堆栈跟踪以查看Exceptionusing er.printStackTrace()。此外,您不应该对字符串路径进行硬编码;使用Environment Class以一致的方式引用文件位置。

于 2013-09-14T16:42:52.543 回答
0

1) 确保您已授予在您的 android 清单文件中读取外部存储的权限。

2)而不是硬编码外部存储的路径使用它。

File sdCardPath = Environment.getExternalStorageDirectory();

然后添加您的文件夹和文件名..

Bitmap bitmap = BitmapFactory.decodeFile(sdCardPath+"/DCIM/Camera/img_folder/xxx.png");
                         jpgView.setImageBitmap(bitmap);
于 2013-09-14T16:51:09.683 回答