Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从Url加载图像会被旋转_Android - Fatal编程技术网

Android 从Url加载图像会被旋转

Android 从Url加载图像会被旋转,android,Android,我试图从url加载图像,但图像被旋转 我已经搜索了来自url的图像的ExiforOrientation,但是失败了 我的图像url是 这张照片是从iPhone摄像头拍摄的,在android设备中旋转 我已经尝试了这个解决方案,但是我认为它对URL不起作用 所以,如何以原始方向表示图像? 提前感谢。如果每次都发生这种情况,您可以在下载后旋转图像。这是我用来解决它的代码 首先下载图片 new GetFileTask(new FileDownloadListener() {

我试图从url加载图像,但图像被旋转

我已经搜索了来自url的图像的ExiforOrientation,但是失败了

我的图像url

这张照片是从iPhone摄像头拍摄的,在android设备中旋转

我已经尝试了这个解决方案,但是我认为它对URL不起作用

所以,如何以原始方向表示图像?


提前感谢。

如果每次都发生这种情况,您可以在下载后旋转图像。

这是我用来解决它的代码

首先下载图片

new GetFileTask(new FileDownloadListener() {

                @Override
                public void onFileDownload(String path) {


                }
            }).execute(image_url, "image_name");

public interface FileDownloadListener{
    public void onFileDownload(String path);

}


public class GetFileTask extends AsyncTask<String, Void, String> {
private FileDownloadListener downloadListner;

public GetFileTask(FileDownloadListener downloadListner) {
    this.downloadListner = downloadListner;
}

@Override
protected void onPostExecute(String path) {
    super.onPostExecute(path);
    // Log.e("FILE_DOWNLOAD", "DOWNLOAD end: " +
    // System.currentTimeMillis());
    File f = new File(path);
    if (f.exists()) {
        downloadListner.onFileDownload(path);
    }
}

/*
 * param 0 : url param 1 : file name without path
 */
@Override
protected String doInBackground(String... params) {
    try {
        // Log.e("FILE_DOWNLOAD",
        // "DOWNLOAD Start: " + System.currentTimeMillis());
        File dir = new File(General.TempImagePath);
        if (!dir.exists()) {
            dir.mkdirs(); // create complete path require to create this
                            // directory
        }

        URL ulrn = new URL(params[0]);
        HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
        InputStream is = con.getInputStream();
        // Log.e("FILE_DOWNLOAD",
        // "DOWNLOAD InputStream: " + System.currentTimeMillis());

        String fileName = (params[1] == null) ? String.valueOf(System
                .currentTimeMillis()) : params[1];

        try {
            OutputStream fOut = null;
            File file = new File(General.TempImagePath, fileName);
            if (file.exists()) {
                file.delete();
            }

            fOut = new FileOutputStream(file);

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = is.read(bytes)) != -1) {
                fOut.write(bytes, 0, read);
            }
            // Log.e("FILE_DOWNLOAD",
            // "DOWNLOAD file-read: " + System.currentTimeMillis());
            is.close();

            fOut.flush();
            fOut.close();

            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}
newgetfiletask(newfiledownloadstener()){
@凌驾
公共无效onFileDownload(字符串路径){
}
}).执行(图像url,“图像名称”);
公共接口FileDownloadListener{
公共void onFileDownload(字符串路径);
}
公共类GetFileTask扩展了AsyncTask{
私有文件下载侦听器下载器;
公共GetFileTask(FileDownloadListener downloadListner){
this.downloadListner=downloadListner;
}
@凌驾
受保护的void onPostExecute(字符串路径){
onPostExecute(路径);
//Log.e(“文件下载”,“下载结束:”+
//System.currentTimeMillis());
文件f=新文件(路径);
如果(f.exists()){
downloadListner.onFileDownload(路径);
}
}
/*
*参数0:url参数1:没有路径的文件名
*/
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
//Log.e(“文件下载”,
//下载开始:“+System.currentTimeMillis());
File dir=新文件(General.TempImagePath);
如果(!dir.exists()){
dir.mkdirs();//创建此文件所需的完整路径
//目录
}
URL ulrn=新URL(参数[0]);
HttpURLConnection con=(HttpURLConnection)ulrn.openConnection();
InputStream=con.getInputStream();
//Log.e(“文件下载”,
//下载输入流:“+System.currentTimeMillis());
字符串文件名=(参数[1]==null)?String.valueOf(系统)
.currentTimeMillis()):参数[1];
试一试{
OutputStream fOut=null;
File File=新文件(General.TempImagePath,文件名);
if(file.exists()){
delete();
}
fOut=新文件输出流(文件);
int read=0;
字节[]字节=新字节[1024];
而((read=is.read(bytes))!=-1){
四次写入(字节,0,读取);
}
//Log.e(“文件下载”,
//下载文件读取:“+System.currentTimeMillis());
is.close();
fOut.flush();
fOut.close();
返回文件.getAbsolutePath();
}捕获(例外e){
e、 printStackTrace();
}
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
}
然后使用它的路径旋转位图,如下所示

public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
    try {
        int orientation = getExifOrientation(src);

        if (orientation == 1) {
            return bitmap;
        }

        Matrix matrix = new Matrix();
        switch (orientation) {
        case 2:
            matrix.setScale(-1, 1);
            break;
        case 3:
            matrix.setRotate(180);
            break;
        case 4:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case 5:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case 6:
            matrix.setRotate(90);
            break;
        case 7:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case 8:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }

        try {
            Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return oriented;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return bitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

private static int getExifOrientation(String src) throws IOException {
    int orientation = 1;

    try {
        /**
         * if your are targeting only api level >= 5 ExifInterface exif =
         * new ExifInterface(src); orientation =
         * exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
         */
        if (Build.VERSION.SDK_INT >= 5) {
            Class<?> exifClass = Class
                    .forName("android.media.ExifInterface");
            Constructor<?> exifConstructor = exifClass
                    .getConstructor(new Class[] { String.class });
            Object exifInstance = exifConstructor
                    .newInstance(new Object[] { src });
            Method getAttributeInt = exifClass.getMethod("getAttributeInt",
                    new Class[] { String.class, int.class });
            Field tagOrientationField = exifClass
                    .getField("TAG_ORIENTATION");
            String tagOrientation = (String) tagOrientationField.get(null);
            orientation = (Integer) getAttributeInt.invoke(exifInstance,
                    new Object[] { tagOrientation, 1 });
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

    return orientation;
}
公共静态位图旋转位图(字符串src,位图位图){
试一试{
int方向=GetExiforOrientation(src);
如果(方向==1){
返回位图;
}
矩阵=新矩阵();
开关(方向){
案例2:
矩阵。设置刻度(-1,1);
打破
案例3:
矩阵旋转(180);
打破
案例4:
矩阵旋转(180);
矩阵。后量表(-1,1);
打破
案例5:
矩阵旋转(90);
矩阵。后量表(-1,1);
打破
案例6:
矩阵旋转(90);
打破
案例7:
矩阵旋转(-90);
矩阵。后量表(-1,1);
打破
案例8:
矩阵旋转(-90);
打破
违约:
返回位图;
}
试一试{
面向位图=位图.createBitmap(位图,0,0,
位图.getWidth(),位图.getHeight(),矩阵,true);
bitmap.recycle();
回归导向;
}捕获(OutOfMemory错误){
e、 printStackTrace();
返回位图;
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回位图;
}
私有静态int-getExiforOrientation(字符串src)引发IOException{
int方向=1;
试一试{
/**
*如果您的目标仅为api级别>=5 ExifInterface exif=
*新ExiFinInterface(src);方向=
*exif.getAttributeInt(ExifInterface.TAG_方向,1);
*/
如果(Build.VERSION.SDK_INT>=5){
类exifClass=Class
.forName(“android.media.ExifInterface”);
构造函数exifConstructor=exifClass
.getConstructor(新类[]{String.Class});
对象exifInstance=exifConstructor
.newInstance(新对象[]{src});
方法getAttributeInt=exifClass.getMethod(“getAttributeInt”,
新类[]{String.Class,int.Class});
字段tagorientionField=exifClass
.getField(“标签方向”);
字符串tagOrientation=(字符串)tagOrientationField.get(null);
方向=(整数)getAttributeInt.invoke(存在状态,
新对象[]{tagOrientation,1});
}
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(安全异常e){
e、 printStackTrace();
}捕获(无此方法例外){
e、 printStackTrace();
}捕获(IllegalArgumentException e){
e、 printStackTrace();
}ca