ImageView不显示特定文件夹中的图像

时间:2013-07-02 09:56:10

标签: android image imageview

当我使用ImageView从SD卡显示图像时,我遇到了问题。我尝试了很多代码,下面的代码,但没有人为我工作,知道活动运行没有任何问题“成功”,但屏幕总是空的,不显示图像。任何的想法。提前谢谢。

activity_mainvie.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".IMAGEVIE" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"/>
</RelativeLayout>

为主要活动尝试的代码是:

public class IMAGEVIE extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagevie);
        ImageView iv=(ImageView) findViewById(R.id.imageview);

        Bitmap bMap = BitmapFactory.decodeFile("/sdcard/life.jpg");
        iv.setImageBitmap(bMap);
    }
}

第二个代码:

 public class IMAGEVIE extends Activity {

    private File mFichier;
    private Uri fileUri;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagevie);
        ImageView iv=(ImageView) findViewById(R.id.imageview);
        File f= new File("/sdcard/life.jpg");
        Bitmap bMap = BitmapFactory.decodeFile(f.getAbsolutePath());
        iv.setImageBitmap(bMap);
    }
}

第三个代码:

public class IMAGEVIE extends Activity {

    private File mFichier;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagevie);
        ImageView iv=(ImageView) findViewById(R.id.imageview);  
        mFichier = new File(Environment.getExternalStorageDirectory(),"life.jpg");

        if(mFichier.exists())
        {
            iv.setImageURI(Uri.fromFile(mFichier));
        }
    }
}

4 个答案:

答案 0 :(得分:1)

首先:您将xml文件另存为activity_main.xml

所以将setContentView更改为: -

的setContentView(R.layout的 activity_main 即可。);

在你的java文件中!

<强>大于 试试这个:

  File f = new File(strImgPath);
        if (f.exists()) {
            Drawable d = Drawable.createFromPath(strImgPath);
            iv.setImageDrawable(d);

        }

为我工作

其中 strImgPath是一个字符串变量,其中包含图像的路径!

并检查你是否在AndroidManifest.xml文件中添加了权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

答案 1 :(得分:1)

你的SD卡路径错误应该是这样的:

 File f= new File("/mnt/sdcard/life.jpg"); 

答案 2 :(得分:0)

将您的内容视图设置为R.layout.activity_main

答案 3 :(得分:0)

首先保存图片:

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
String sdCard = Environment.getExternalStorageDirectory().toString(); File myDir = new File(sdCard + "/folder");
myDir.mkdir();
FSApi.snapPic(myDir+"/img");
}

然后打开它:

            File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/folder/");
            allFiles = folder.listFiles();
            if (allFiles == null) {

                AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();
                alertDialog.setTitle("Alert");
                alertDialog.setMessage("No such a file/directory");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                }
            });
                alertDialog.show();

            }
            // Folder is empty
            else if (allFiles.length == 0) {

                AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();
                alertDialog.setTitle("Alert");
                alertDialog.setMessage("No snapshot to display");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                }
            });
                alertDialog.show();

            }
            // Folder contains files
            else {
                new SingleMediaScanner(Activity.this, allFiles[0]);
            }
        }


public class SingleMediaScanner implements MediaScannerConnectionClient {

    private MediaScannerConnection mMs;
    private File mFile;

    public SingleMediaScanner(Context context, File f) {
        mFile = f;
        mMs = new MediaScannerConnection(context, this);
        mMs.connect();
    }

    public void onMediaScannerConnected() {
        mMs.scanFile(mFile.getAbsolutePath(), null);
    }

    public void onScanCompleted(String path, Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        startActivity(intent);
        mMs.disconnect();
    }
}