GVKun编程网logo

android – 如何在SD卡中保存GIF图像?(sd卡怎么保存图片)

5

最近很多小伙伴都在问android–如何在SD卡中保存GIF图像?和sd卡怎么保存图片这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展android–ACRA:如何将ACRA报告写

最近很多小伙伴都在问android – 如何在SD卡中保存GIF图像?sd卡怎么保存图片这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展android – ACRA:如何将ACRA报告写入文件(在SD卡中)?、android – 卸载我的应用程序后如何从SD卡中删除文件、android – 在SD卡中移动文件、android – 如何从存储在SD卡上的图像中获取图像路径等相关知识,下面开始了哦!

本文目录一览:

android – 如何在SD卡中保存GIF图像?(sd卡怎么保存图片)

android – 如何在SD卡中保存GIF图像?(sd卡怎么保存图片)

我是新的android,我想通过android编程保存sdcard中的gif图像.目前我已经从谷歌做了一些代码来保存SD卡中的gif图像.但是当我将该图像保存到SD卡时,它将显示正常图像而不是gif图像.

这是我显示gif图像的代码

//Save code
    save.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bitmap image = BitmapFactory.decodeResource(getResources(),
                    R.drawable.gpp3);
            File outputFile = new File("/sdcard/gpp3.gif");
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(outputFile);
            } catch (FileNotFoundException e) {
                e.printstacktrace();
            }

            if (fos != null) {
                AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder();
                gifEncoder.start(fos);
                gifEncoder.addFrame(image);
                gifEncoder.finish();
            }

        }
    });

那么,上面的代码有什么问题.请告诉我.

解决方法:

我不确定,但根据您的要求,您应首先打开您的GIF,然后在转换为字节数组后再保存它.我希望您能得到您的解决方案

private void saveGIF()
    {
        try
        {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "shared_gif_shai" + System.currentTimeMillis() + ".gif");

            long startTime = System.currentTimeMillis();

            Log.d(TAG, "on do in background, url open connection");

            InputStream is = getResources().openRawResource(R.drawable.g);
            Log.d(TAG, "on do in background, url get input stream");
            BufferedInputStream bis = new BufferedInputStream(is);
            Log.d(TAG, "on do in background, create buffered input stream");

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Log.d(TAG, "on do in background, create buffered array output stream");

            byte[] img = new byte[1024];

            int current = 0;

            Log.d(TAG, "on do in background, write byte to baos");
            while ((current = bis.read()) != -1) {
                baos.write(current);
            }


            Log.d(TAG, "on do in background, done write");

            Log.d(TAG, "on do in background, create fos");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());

            Log.d(TAG, "on do in background, write to fos");
            fos.flush();

            fos.close();
            is.close();
            Log.d(TAG, "on do in background, done write to fos");
        }
        catch (Exception e) {
            e.printstacktrace();
        }
    }

并且还在您的AndroidMenifest.xml文件中授予此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

android – ACRA:如何将ACRA报告写入文件(在SD卡中)?

android – ACRA:如何将ACRA报告写入文件(在SD卡中)?

我可以通过处理未捕获的异常来使用ACRA库来管理强制关闭错误.该报告可以成功发送到谷歌文档,电子邮件和自定义Web服务..

但是我想要的……

>我如何将报告写入文件[例如. sdcard / myapp / myLog.txt]?

为什么我要这个..

>我的应用程序用户可能在强制关闭时没有互联网连接..如果是,那么我将错过报告,如果我将报告写入文件然后我可以在互联网连接可用时发送到我的服务器.

解决方法

我想您想要实现的目标已经由ACRA完成.这是我在abd logcat中看到的内容:
01-23 12:15:28.056: D/ACRA(614): Writing crash report file.
01-23 12:15:28.136: D/ACRA(614): Mark all pending reports as approved.
01-23 12:15:28.136: D/ACRA(614): Looking for error files in /data/data/com.ybi/files
01-23 12:15:28.136: V/ACRA(614): About to start ReportSenderWorker from #handleException
01-23 12:15:28.146: D/ACRA(614): Add user comment to null
01-23 12:15:28.146: D/ACRA(614): #checkAndSendReports - start
01-23 12:15:28.146: D/ACRA(614): Looking for error files in /data/data/com.ybi/files

ACRA所做的第一件事就是在应用程序的内部存储上创建一个文件报告.
然后,如果您在线并且错误报告器已正确初始化,则会发送报告.
否则,报告将保留在数据存储中(以便稍后发送).

我没有查看数据,但我正在研究自定义记录器.因此,如果您想要做与ACRA相同的事情,那很简单:

ACRA.init(this);

    // a custom reporter for your very own purposes
    ErrorReporter.getInstance().setReportSender(new LocalReportSender(this));

然后 :

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.acra.ACRA;
import org.acra.CrashReportData;
import org.acra.ReportField;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;

import android.content.Context;

import de.akquinet.android.androlog.Log;

public class LocalReportSender implements ReportSender {

private final Map<ReportField,String> mMapping = new HashMap<ReportField,String>() ;
private FileOutputStream crashReport = null; 

public LocalReportSender(Context ctx) {
    // the destination
    try {
        crashReport = ctx.openFileOutput("crashReport",Context.MODE_WORLD_READABLE);
    } catch (FileNotFoundException e) {
        Log.e("TAG","IO ERROR",e);
    }
}

@Override
public void send(CrashReportData report) throws ReportSenderException {

    final Map<String,String> finalReport = remap(report);

    try {
        OutputStreamWriter osw = new OutputStreamWriter(crashReport);

        Set set = finalReport.entrySet();
        Iterator i = set.iterator();

        while (i.hasNext()) {
            Map.Entry<String,String> me = (Map.Entry) i.next();
            osw.write("[" + me.getKey() + "]=" + me.getValue());
        }

        osw.flush();
        osw.close();
    } catch (IOException e) {
        Log.e("TAG",e);
    }

}

private static boolean isNull(String aString) {
    return aString == null || ACRA.NULL_VALUE.equals(aString);
}

private Map<String,String> remap(Map<ReportField,String> report) {

    ReportField[] fields = ACRA.getConfig().customreportContent();
    if (fields.length == 0) {
        fields = ACRA.DEFAULT_REPORT_FIELDS;
    }

    final Map<String,String> finalReport = new HashMap<String,String>(
            report.size());
    for (ReportField field : fields) {
        if (mMapping == null || mMapping.get(field) == null) {
            finalReport.put(field.toString(),report.get(field));
        } else {
            finalReport.put(mMapping.get(field),report.get(field));
        }
    }
    return finalReport;
}

}

我还没有完全测试它,但你明白了.希望能帮助到你.

android – 卸载我的应用程序后如何从SD卡中删除文件

android – 卸载我的应用程序后如何从SD卡中删除文件

我正在开发应用程序,它将在SD卡中创建一些文本文件并将一些数据存储到其中.

这些文件将一直存在于SD卡中.

每当用户卸载我的应用程序时,我想删除我的应用程序创建的SD卡中的所有文件.

我无法在onDestroy()中清理,因为它将在应用程序生命周期中多次调用.

请建议我必须做些什么来确保在没有任何用户交互的情况下删除我的文件.

我知道getExternalCacheDir()会做我需要的,但我的文件很大,我需要SD卡的1GB空间,
getExternalCacheDir()对我有用吗?

解决方法:

您应该使用API​​参考中的Context.getExternalFilesDir():

“This is like getFilesDir() in that these files will be deleted when
the application is uninstalled”

android – 在SD卡中移动文件

android – 在SD卡中移动文件

我一直试图在SD卡中移动文件,但无济于事:这是代码:

try {
            File sd=Environment.getExternalStorageDirectory();
            // File (or directory) to be moved
            String sourcePath="mnt/sdcard/.Images/"+imageTitle;
            File file = new File(sd,sourcePath);
            // Destination directory
            String destinationPath="mnt/sdcard/"+imageTitle;
            File dir = new File(sd,destinationPath);

            // Move file to new directory
            boolean success = file.renameto(new File(dir, file.getName()));
            if (!success) {
                handler.post(new Runnable(){
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "File moved", Toast.LENGTH_LONG).show();
                    }
                });
            }

        } 
        catch (Exception e) {
        }

我不知道wasup.Will欣赏帮助.

解决方法:

第一:如果您已获得外部目录,则无需将其添加到源路径和目标路径的开头

其次,destinationPath似乎没必要,因为它看起来只是想将它移动到sdcard的根文件夹.

它应该是

File sd=Environment.getExternalStorageDirectory();
// File (or directory) to be moved
String sourcePath="/.Images/"+imageTitle;
File file = new File(sd,sourcePath);
// Destination directory
boolean success = file.renameto(new File(sd, imageTitle));

android – 如何从存储在SD卡上的图像中获取图像路径

android – 如何从存储在SD卡上的图像中获取图像路径

是否有可能获得存储在我的 Android手机的SD卡上的所有图像的路径?还可以检查存储在SD卡或内部存储器中的其他图像吗?我目前正在这样做:

Cursor cur = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null,null);  
        startManagingCursor(cur);  
        cur.movetoFirst();
        while (cur.movetoNext()) {
            String str = cur.getString(cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_disPLAY_NAME));
            Log.e("#########",str);
        }

这似乎就像返回目录名,
我也见过这段代码:

File images = Environment.getDataDirectory();  
        File[] imagelist = images.listFiles(new FilenameFilter(){  
        public boolean accept(File dir,String name)  
        {  
            return ((name.endsWith(".jpg"))||(name.endsWith(".png")));  
        }  
    });  
        String []mFiles = new String[imagelist.length];  

        for(int i= 0 ; i< imagelist.length; i++)  
        {  
            mFiles[i] = imagelist[i].getAbsolutePath();  
        }  
        Uri[] mUrls = new Uri[mFiles.length];  

        for(int i=0; i < mFiles.length; i++)  
        {  
            mUrls[i] = Uri.parse(mFiles[i]);  
            Log.e("###############","MURIS: "+mUrls[i].getEncodedpath());
        }

但这是抛出一个nullpointer异常.

解决方法

您可以使用FileFilter.这是我写的一个用于返回包含图像的目录列表.从这里,修改它以返回图像列表应该相当简单:

FileFilter filterForImageFolders = new FileFilter() 
    {            
        public boolean accept(File folder) 
        { 
            try 
            { 
                //Checking only directories,since we are checking for files within 
                //a directory 
                if(folder.isDirectory()) 
                { 
                    File[] listofFiles = folder.listFiles(); 

                    if (listofFiles == null) return false; 

                    //For each file in the directory... 
                    for (File file : listofFiles) 
                    {                            
                        //Check if the extension is one of the supported filetypes                           
                        //imageExtensions is a String[] containing image filetypes (e.g. "png")
                        for (String ext : imageExtensions) 
                        { 
                            if (file.getName().endsWith("." + ext)) return true; 
                        } 
                    }                        
                } 
                return false; 
            } 
            catch (SecurityException e) 
            { 
                Log.v("debug","Access Denied"); 
                return false; 
            } 
        } 
    };

编辑:澄清一下,使用它,你会做如下的事情:

File extStore = Environment.getExternalStorageDirectory();
File[] imageDirs = extStore.listFiles(filterForImageFolders);

今天的关于android – 如何在SD卡中保存GIF图像?sd卡怎么保存图片的分享已经结束,谢谢您的关注,如果想了解更多关于android – ACRA:如何将ACRA报告写入文件(在SD卡中)?、android – 卸载我的应用程序后如何从SD卡中删除文件、android – 在SD卡中移动文件、android – 如何从存储在SD卡上的图像中获取图像路径的相关知识,请在本站进行查询。

本文标签: