如何打开保存在drawable中的图像?

时间:2013-12-04 09:24:25

标签: android

我正在尝试打开一个figure603.png图像,我已经在res / drawable-hdpi,res / drawable-ldpi,res / drawable-mdpi和res / drawable-xhdpi

这是我的程序,

package com.rufflez.swipeytabs;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.view.View.OnClickListener;

import com.actionbarsherlock.app.SherlockFragment;

public class Fragment_6 extends SherlockFragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View v =inflater.inflate(R.layout.fragment_6, container, false); // inflate the layout
    TextView tv = (TextView) v.findViewById(R.id.textView1); // initialize textview using inflated view object 

    tv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {           
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("android.resource://com.rufflez.swipeytabs/" +  R.drawable.figure603));
    startActivity(intent);
    }
    }); 
    return v;
}

}

这是我的logcat

    12-04 04:32:36.941: D/dalvikvm(1271): GC_FOR_ALLOC freed 55K, 4% free 2998K/3116K, paused 67ms, total 68ms
    12-04 04:32:36.961: I/dalvikvm-heap(1271): Grow heap (frag case) to 4.061MB for 1127536-byte allocation
    12-04 04:32:37.011: D/dalvikvm(1271): GC_FOR_ALLOC freed <1K, 3% free 4099K/4220K, paused 55ms, total 55ms
    12-04 04:32:37.401: V/21st Polling:(1271): clicked
    12-04 04:32:37.811: D/(1271): HostConnection::get() New Host Connection established 0xb8973b10, tid 1271
    12-04 04:32:37.921: W/EGL_emulation(1271): eglSurfaceAttrib not implemented
    12-04 04:32:37.941: D/OpenGLRenderer(1271): Enabling debug mode 0
    12-04 04:32:55.901: V/21st Polling:(1271): clicked
    12-04 04:32:57.791: D/AndroidRuntime(1271): Shutting down VM
    12-04 04:32:57.791: W/dalvikvm(1271): threadid=1: thread exiting with uncaught exception (group=0xb1a77b90)
    12-04 04:32:57.831: E/AndroidRuntime(1271): FATAL EXCEPTION: main
    12-04 04:32:57.831: E/AndroidRuntime(1271): Process: com.rufflez.swipeytabs, PID: 1271
    12-04 04:32:57.831: E/AndroidRuntime(1271): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.rufflez.swipeytabs/2130837616 }
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.app.Activity.startActivityForResult(Activity.java:3423)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.app.Activity.startActivityForResult(Activity.java:3384)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:683)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.support.v4.app.Fragment.startActivity(Fragment.java:783)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at com.rufflez.swipeytabs.Fragment_6$1.onClick(Fragment_6.java:28)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.view.View.performClick(View.java:4424)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.view.View$PerformClick.run(View.java:18383)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.os.Handler.handleCallback(Handler.java:733)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.os.Handler.dispatchMessage(Handler.java:95)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.os.Looper.loop(Looper.java:137)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at android.app.ActivityThread.main(ActivityThread.java:4998)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at java.lang.reflect.Method.invoke(Method.java:515)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    12-04 04:32:57.831: E/AndroidRuntime(1271):     at dalvik.system.NativeStart.main(Native Method)
    12-04 04:33:00.481: I/Process(1271): Sending signal. PID: 1271 SIG: 9

当我点击文字时,假设打开figure603.png,但它会给我一个力量关闭。 可能是什么问题?

2 个答案:

答案 0 :(得分:0)

您使用的是错误的URI

 Uri uri=Uri.parse("android.resource://your.package.here/drawable/image_name");

 intent.setData(Uri.parse("android.resource://your.package.here/drawable/image_name");

答案 1 :(得分:0)

您应该使用以下代码:

intent.setData(Uri.parse("android.resource://com.rufflez.swipeytabs/drawable/figure603"));

而不是:

intent.setData(Uri.parse("android.resource://com.rufflez.swipeytabs/" +  R.drawable.figure603));

我不确定这一点,但是你不需要setData的输入流吗?如果你这样做将是代码:

InputStream stream = getContentResolver().openInputStream(uri);