上传图片到apk

时间:2013-12-27 11:15:57

标签: android image apk android-imageview

我想将图片从图库上传到apk。如何在没有Web服务的情况下附加图像?

我应该为此内容提供商使用sqlite吗?

附加图像后,我希望它在图像查看器中显示。

1 个答案:

答案 0 :(得分:1)

好的,先做一些事情。您在图库中看到的图像存储在手机记忆库或SD卡中。您存储在应用程序内的资源文件夹中的图像(eclipse中的资源文件夹)供内部应用程序使用,当构建apk时,您不能放置任何内容。你想要的是在运行时将一些图像与你的应用相关联,这样就有一些方法可以做到这一点你只需选择适合你的应用。

首先,您需要通过Intent从应用程序中打开图像库,如下所示:

//Constant to compare result
private static int RESULT_LOAD_IMAGE = 1;
//Intent that call the gallery    
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//Start another activity that returns a result to yours
startActivityForResult(i, RESULT_LOAD_IMAGE);

//Override the callback to get the result for the activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //If everythig ok and a image is selected
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        //Get the image path
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        //Show the image in your ImageView
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

好了,现在你有你的图像,你想决定如何处理它,那就是我想你想要的“在我的apk上传”。您现在拥有的图像位于记忆手机或SD卡中,现在您可以看到该图像的路径。如果要复制它并将其存储在您的应用程序中,您可以将图像复制到应用程序专用文件夹,更改图像名称并以您希望的方式存储新路径(sqlite,sharedPrefs,将名称更改为某些逻辑,您可以获取图像名称等)现在图像被“上传”到您的应用程序。要获取应用私人文件夹,请使用Context.getFilesDir()。但是你已经拥有了图像文件,只需用它做你想做的事。并且要小心权限。其他方式是在服务器中存储图像的Web服务,sqlite将图像存储在数据库中。