将flickr图像保存到GridView中显示的SD卡

时间:2015-11-05 04:22:46

标签: android gridview sd-card flickr

到目前为止,我已经在我的Android应用程序中进行了flickr集成。

我已将来自flickr帐户的图库和照片流的所有图片显示到GridView

现在,我必须将图像保存到设备,即sdcard

我尝试过以下gvPhotosGridView的代码:

gvPhotos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Drawable drawable= null;

            drawable = view.getDrawable();


            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

            String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();

            File file = new File(path, "name.png");

            FileOutputStream out = null;
            try {
                out = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });

但是,它给出了错误

  

无法解析方法getDrawable()

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PhotosActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridView
            android:id="@+id/gvPhotos"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="3"></GridView>

        <ImageView
            android:id="@+id/selectedImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/gvPhotos"
            android:layout_centerHorizontal="true"/>
    </RelativeLayout>
</ScrollView>

我怎样才能完成这些工作?

2 个答案:

答案 0 :(得分:2)

IMO,您不应该尝试从位图中保存文件,因为它可能会缩小,具体取决于您用于显示位图的缩放算法。结果可能是质量下载文件质量不佳。相反,您应该再次从HTTP服务器下载文件:

private boolean downloadFile(String uri, String fileName) throws URISyntaxException, IOException  {

    URL url = new URL(uri);

    InputStream input = null;
    OutputStream output = null;
    HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        //Check for HTTP_OK on the url
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Log.d(TAG, "Server returned HTTP " + connection.getResponseCode()
                    + " " + connection.getResponseMessage());
            return false;
        }

        long totalSize = connection.getContentLength();

        // download the file
        input = connection.getInputStream();
        output = new FileOutputStream(fileName);

        byte data[] = new byte[1024 * 50];
        long downloadedBytes = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            // allow canceling with back button
            if (isCancelled()) {
                input.close();
                return false;

            }
            downloadedBytes += count;
            //There may be a problem that the size is reported negative by the server. This is to protect for that
            if(totalSize > 0){
               publishProgress((int) (downloadedBytes * 100 / totalSize));
            }
            output.write(data, 0, count);
        }
    } catch (Exception e) {

        e.printStackTrace();
        return false;
    } finally {
        try {
            if (output != null)
                output.close();
            if (input != null)
                input.close();
        } catch (IOException ignored) {
        }

        if (connection != null)
            connection.disconnect();
    }
    return true;
}

将其包装在异步任务中,并在itemClick()上调用此异步任务。或者,在下载的服务中使用它,使文件下载操作独立于您的活动

答案 1 :(得分:1)

错误:

  

无法解析方法getDrawable()

从错误中我可以说您在getDrawable()上调用view方法。

首先,您必须获得Clicked item的ImageView引用,然后使用ImageView.getDrawable()

你在onItemClick()方法中做错了。

您正在致电

drawable = view.getDrawable();

不会返回Drawable

修改

你可以试试这个

Drawable drawable= null;
ImageView im = (ImageView) view.findViewById(R.id.imageViewXYZ);
drawable = im.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

其中imageViewXYZ是您的GridView内部布局ImageView的ID。

您可以使用此方法将图像保存到SD卡。

public void saveImageToDevice(Bitmap bm) {
        try {
           String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
            File directory = new File(path);
            directory.mkdirs();
            String filename = "SAMPLE.png";
            File file = new File(path, filename);
            if (file.exists()) {
                file.delete();
            } else {
                OutputStream fOut;
                fOut = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                fOut.flush();
                fOut.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}

我希望它可以帮到你。