0

当我使用 ImageView 显示来自 sdcard 的图像时,我遇到了问题。我尝试了许多代码,如下所示,但没有人为我工作,知道活动运行没有任何问题“成功”,但屏幕始终为空且不显示图像。任何的想法。提前致谢。

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

4 回答 4

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"/>
于 2013-07-02T10:07:41.650 回答
1

你的 sdcard 路径是错误的,应该是这样的:

 File f= new File("/mnt/sdcard/life.jpg"); 
于 2013-07-02T10:22:08.877 回答
0

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

于 2013-07-02T10:06:42.910 回答
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();
    }
}
于 2013-07-02T12:00:11.363 回答