下载图片在服务失败SD卡(安卓)下载图片、SD

2023-09-05 10:35:19 作者:给你最后的疼爱是手放开

我创建了一个单独的项目和活动的下载一个图像从一个网址,将其转换为位图,然后使用一个FileOutputStream到该文件保存到SD卡上。当在单独的项目和自由站立活动,这工作得很好,我可以看到图像存储在SD卡上。

I created a separate project and activity that downloads an image from a URL, converts it to a Bitmap, and then uses a FileOutputStream to save that file to the SD card. When in the separate project and free-standing activity, this worked fine, and I could see that the image is stored on the SD card.

public class PictureDownload extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        downloadFromUrl("https://m.xsw88.com/allimgs/daicuo/20230905/2179.png.jpg", "newpic.jpg");
        displayPic("newpic.jpg");
    }

    // Place to download the file
    private final String PATH = "/sdcard/";

    public void displayPic(String filename) {
        ImageView image = (ImageView)findViewById(R.id.image);
        Bitmap bMap = BitmapFactory.decodeFile(PATH + filename);
        image.setImageBitmap(bMap);
    }

    /**
     * Downloads the picture from the URL to the SD card
     * @param imageURL: the URL to download it from (absolute web URL)
     * @param fileName: the name of the file you want to save the picture to
     */
    public void downloadFromUrl(String imageURL, String fileName) {
        try {
                // Connect to the URL
                URL myImageURL = new URL(imageURL);
                HttpURLConnection connection = (HttpURLConnection)myImageURL.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();

                // Get the bitmap
                Bitmap myBitmap = BitmapFactory.decodeStream(input);

                // Save the bitmap to the file
                String path = Environment.getExternalStorageDirectory().toString();
                OutputStream fOut = null;
                File file = new File(path, fileName);
                fOut = new FileOutputStream(file);

                myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                fOut.flush();
                fOut.close();
            } catch (IOException e) {}
    }

}

不过,每当我试图端口code到现有项目,它失败。在这种背景下,我正在从服务的code。在code是完全相同的 - 但由于某些原因,它不会下载任何文件。请问这有什么关系,它是一个服务,而不是一个活动运行的事实?或者,也许出于某种原因,文件输出流被搞砸了。

But, whenever I tried to port the code to an existing project, it fails. In this setting, I am running the code from a service. The code is the exact same - but for some reason it won't download any files. Would this have to do with the fact that it is running on a service instead of an activity? Or maybe for some reason the file output stream gets messed up.

在BGService:

In BGService:

String name = remote_resource.getValue().substring(38);
                downloadFromUrl(remote_resource.getValue(), name);
 /**
 * Downloads the picture from the URL to the SD card
 * @param imageURL: the URL to download it from (absolute web URL)
 * @param fileName: the name of the file you want to save the picture to
 */
public void downloadFromUrl(String imageURL, String fileName) {
    try {
            // Connect to the URL
            URL myImageURL = new URL(imageURL);
            HttpURLConnection connection = (HttpURLConnection)myImageURL.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();

            // Get the bitmap
            Bitmap myBitmap = BitmapFactory.decodeStream(input);

            // Save the bitmap to the file
            String path = Environment.getExternalStorageDirectory().toString();

            OutputStream fOut = null;
            File file = new File(path, fileName);
            Log.i("help", file.getAbsolutePath());
            Log.i("help", file.toString());
            Log.i("help", file.length() + "");
            System.out.println("THIS IS A TEST OF SYSTEM.OUT.PRINTLN()");
            fOut = new FileOutputStream(file);

            myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
            fOut.close();
            System.out.println("file Path: " + file.getAbsolutePath());
            Log.i("help2", file.getAbsolutePath());
            Log.i("help2", file.toString());
            Log.i("help2", file.length() + "");

        } catch (IOException e) {}
}

另一个有趣的事情是,Log.i来电下面,只有第二个(下面的fOut.flush())被打印出来。如此在该位COM pression或FileOutputStream中IO奇怪的事情正在发生。但是,没有抛出异常。

Another interesting thing is that of the "Log.i" calls below, only the second one (below the fOut.flush()) gets printed. So somewhere in the bitmap compression or the FileOutputStream IO something weird is happening. But no exceptions are thrown.

非常感谢您的帮助!

推荐答案

您确定你确实有该WRITE_EXTERNAL_STORAG​​E启用。如果您使用的是permission-group.STORAG​​E它并不总是包含在里面这一点。

Are you sure you actually have the WRITE_EXTERNAL_STORAGE is enabled. If you are using the permission-group.STORAGE it doesn't always include this in there.

 
精彩推荐
图片推荐