从存储卡读取图像

时间:2013-08-20 14:58:55

标签: android

此代码从android:

的DRAWABLE文件夹中读取图像
import android.widget.ImageView;
import android.widget.ZoomControls;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class info extends Activity {
    Bitmap bmp = BitmapFactory.decodeFile("sdcard/THQ/Para01/page1.jpg");
    ImageView img;
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    PointF startPoint = new PointF();
    PointF midPoint = new PointF();
    float oldDist = 1f;
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    // img.setImageBitmap(bmp);
    private int imCurrentPhotoIndex = 0;

    private int[] imPhotoIds = new int[] { R.drawable.info, R.drawable.info1,
            R.drawable.info2, R.drawable.info3, R.drawable.info4,
            R.drawable.info5, R.drawable.info6 };

    Button ibd1, ibd2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // fullscreen starts
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // fullscreen code ends
//img.setb
        setContentView(R.layout.info);
        showPhoto(imCurrentPhotoIndex);

        ibd1 = (Button) findViewById(R.id.next);
        ibd2 = (Button) findViewById(R.id.prev);



        ibd1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                imCurrentPhotoIndex++;

                if (imCurrentPhotoIndex < 7)
                    showPhoto(imCurrentPhotoIndex);
                else
                    imCurrentPhotoIndex--;

                // ///

            }
        });

        ibd2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                imCurrentPhotoIndex--;

                if (imCurrentPhotoIndex > -1)
                    showPhoto(imCurrentPhotoIndex);
                else
                    imCurrentPhotoIndex++;

                // ///

            }
        });

    }

    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("photo_index", imCurrentPhotoIndex);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        imCurrentPhotoIndex = savedInstanceState.getInt("photo_index");
        showPhoto(imCurrentPhotoIndex);
        super.onRestoreInstanceState(savedInstanceState);
    }

    private void showPhoto(int photoIndex) {
        ImageView imageView = (ImageView) findViewById(R.id.image_view);
        // imageView.setImageResource(imPhotoIds[photoIndex]);
        imageView.setImageResource(imPhotoIds[photoIndex]);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.starting_point, menu);
        return true;
    }

}

请您帮我翻阅并显示文件夹“sdcard\THQ\something.png”中的图片。

2 个答案:

答案 0 :(得分:0)

首先,您使用此代码

从文件夹中获取图像列表
ArrayList<String> pathList = new ArrayList<String>();// 
File[] listFile;

public void getFromSdcard()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"YOUR_FOLDER_NAME");

        if (file.isDirectory())
        {
            listFile = file.listFiles();


            for (int i = 0; i < listFile.length; i++)
            {

                pathList.add(listFile[i].getAbsolutePath());

            }
        }
}

然后您可以使用BitmapFactory显示解码并将其显示到图像视图

Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(index));
imageview.setImageDrawable(bitmap);

我希望我的回答可以帮助你:)

答案 1 :(得分:0)

    Bitmap bmp = BitmapFactory.decodeFile("mnt\\sdcard\\THQ\\Info\\info.jpg");
ImageView img = (ImageView) findViewById(R.id.image_view);
        img.setImageBitmap(bmp);

这就是我们需要从存储卡的文件夹中显示照片;