0

我正在尝试使用隐式 Intent 通过手机的相机图像查看器应用程序显示不同的伦敦地铁地图。我在资产文件夹中有tube_map.gif图像文件,但是当我尝试加载此文件时,应用程序显示无法找到项目。我认为我指定的文件路径不正确。我已经关注了以下视频。唯一的区别是,在视频中,图像文件存储在手机的 SD 卡中,而在我的情况下,它存储在资产文件夹中。视频可以通过这个链接看到。我的代码如下:

package uk.ac.kingston.mobileTechnology.k1059045.trainCountdown;

import java.io.File;

import uk.ac.kingston.mobileTechnology.k1059045.trainCountdown.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Toast;

public class ViewTubeMap extends Activity{

String[] maps = {"TUBE","NATIONAL RAIL","OVERGROUND","DLR","TRAMLINK","RIVER BUS","TOURISTS (Tube Map)","TOURISTS (Bus Map)","RAIL CONNECTIONS","TOILET FACILITIES"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.tube_map_layout);

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a Map");
               builder.setItems(maps, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                   // The 'which' argument contains the index position
                   // of the selected item
                       switch(which){

                       case(0):
                           makeToast("Case 0");
                           Intent intent = new Intent();
                                   intent.setAction(android.content.Intent.ACTION_VIEW);
                           File image = new File("assets/tube_map.gif");
                           intent.setDataAndType(Uri.fromFile(image), "image/*");
                           startActivity(intent);
                           break;

                       case(1):
                           makeToast("Case 1");
                           break;
                       case(2):
                           makeToast("Case 2");
                           break;
                       case(3):
                           makeToast("Case 3");
                           break;
                       case(4):
                           makeToast("Case 4");
                           break;
                       case(5):
                           makeToast("Case 5");
                           break;
                       case(6):
                           makeToast("Case 6");
                           break;
                       case(7):
                           makeToast("Case 7");
                           break;
                       case(8):
                           makeToast("Case 8");
                           break;
                       case(9):
                           makeToast("Case 9");
                           break;
                       }
               }
        });

       builder.create();
       builder.show();
}

public void makeToast(String message) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

谁能指导我我的代码有什么问题,或者我如何访问我的图像并将其显示在手机的相机图像查看应用程序中?谢谢

4

1 回答 1

1

assets 是应用程序的私有区域,所以幸运的是,没有其他应用程序可以访问另一个应用程序的这个区域。我要么通过 SD 卡(选项 1)解决问题,要么通过互联网(不是本地)提供 tubeMaps 并从那里获取图像(选项 2)。

选项 1:您添加访问外部存储的权限。在活动开始时,您将图像从本地资产同步到外部存储,然后稍微更改代码以传递另一个文件对象以及指向外部存储的 ACTION_VIEW 意图。

选项 2:您将管图上传到 Internet 上的某个位置,并为 ACTION_VIEW 意图提供一个 url。

于 2013-04-11T08:17:58.557 回答