通过邮件请求发送.dex文件android studio

时间:2016-11-17 14:49:21

标签: file android-studio post dex

出于我自己的目的,我想将其他应用程序的dex文件发送到远程服务器并获得答案。

我使用过我在这里找到的答案。我首先尝试创建一个简单的示例,只是为了连接到服务器并上传dex文件。到目前为止,我还没有设法从其他应用程序中提取dex,所以我想到了使用我已经拥有的dex文件。 正如我所读到的,不应将常见文件存储到“/ res / raw”或“assets” 我尝试了很多方法将它作为文件加载,但它们都没有工作。我在所有情况下使用的路径都是在右键单击文件中找到的 - >复制参考。

  1. 在/ raw和

    下创建一个res文件夹
    File f = new File("res/raw/filename.dex");
    
  2. 在/ app

    下创建一个新的资源文件夹
    File f = new File("filename.dex");
    
  3. 在/ main

    下创建资源文件夹
    File f = new File("main/assets/filename.dex");
    

    等等。

  4. 我设法做到的唯一方法是使用InputStream

    Inputstream in = getResources().openRawResources(R.raw.filename_without_dex)
    

    但我无法将其强制转换为File,因此我删除了此解决方案。我希望将其作为文件,因为以下POST请求必须是多部分/表单。

    在java中,“加载”文件的方式很简单。为什么不在android?

1 个答案:

答案 0 :(得分:0)

一个真正丑陋,快速和肮脏的解决方案 没有错误检查或清理。
但它确实有效。

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class MainActivity extends ActionBarActivity {
    final String TAG = "GetDex";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getAppPaths(); // to list all packages

        String s = "/data/app/SoftKeyboard/SoftKeyboard.apk"; // a sample package
        byte[] b = getFile(s);
        byte[] dex = unzipDex(b, s);
        Log.d(TAG, String.format("DEX size is %d", dex.length));
    }





List<String> getAppPaths() {
    final PackageManager pm = getPackageManager();
    //get a list of installed apps.
    List<ApplicationInfo> packages =  pm.getInstalledApplications(PackageManager.GET_META_DATA);
    List<String> paths = new ArrayList<>();

    for (ApplicationInfo packageInfo : packages) {
        Log.d(TAG, "Installed package :" + packageInfo.packageName);
        Log.d(TAG, "Apk file path:" + packageInfo.sourceDir);
        paths.add(packageInfo.sourceDir);
    }

    return paths;
}


byte[] getFile(String filename) {
    try {
        RandomAccessFile f = new RandomAccessFile(filename, "r");
        byte[] b = new byte[(int)f.length()];
        f.readFully(b);
        return b;
    } catch(IOException exception) {
        exception.printStackTrace();
    }

    return null;
}

public byte[] unzipDex(byte[] bytes, String filename) {
    try{
        ZipFile zipFile = new ZipFile(filename);
        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bytes));
        ZipEntry ze = zis.getNextEntry();

        while(ze!=null){
            String entryName = ze.getName();
            if(!entryName.equals("classes.dex")) {
                ze = zis.getNextEntry();
                continue;
            }

            InputStream is = zipFile.getInputStream(ze);

            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            int nRead;
            byte[] data = new byte[16384];

            while ((nRead = is.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }

            buffer.flush();

            return buffer.toByteArray();
        }
        System.out.println("Done");

    }catch(IOException ex){
        ex.printStackTrace();
    }

    return null;
}

}

相关问题