ImageView不显示存储在手机上的jpg

时间:2015-01-03 05:40:17

标签: android image bitmap android-imageview filepath

我非常喜欢这个,所以如果之前已经讨论过,我提前道歉并且没有使用正确的搜索条件来找到我的答案。任何帮助是极大的赞赏。

我正在尝试使用后退按钮,下一个按钮,图像文件和每个图像的标题来创建一个非常基本的应用程序,其中按钮用于从一个图像/标题对转到下一个或后面。所有图像都位于手机主目录的“图片”文件夹中,所有标题都是按顺序编号的文本字符串。

我得到的标题是与按钮正确交互,但无法使图像正常工作。我甚至无法在ImageView中显示第一个图像,可能是因为我使用了错误的文件路径或错误的图像命令。

jpgs的编号从0到25,简单的文件名(0.jpg 1.jpg等)。我制作了一款适用于Android 2.3但却无法解决4.4.4的问题。

我设置我的初始TextView以显示Environment.getExternalStorageDirectory()的结果,希望这会使我的文件路径正确。我已尝试使用该命令并使用返回文件路径的文本,如我在代码中的注释中所示。不幸的是,两者都没有。评论很混乱,因为我是新手,只是为自己保存了可能的代码选项。

我在我的早期版本的android中使用了bitmapDrawable和/ sdcard / file路径。我已经阅读了大约15-20个这些imageView问题尝试不同的解决方案到目前为止没有运气。

我希望这是足够的信息。如果有人可以帮助我,我真的很感激。

public class MainActivity extends ActionBarActivity {
ImageView image;
Button next02, back02;
TextView output01;
int test = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    output01 = (TextView) findViewById(R.id.output);
    next02 = (Button) findViewById(R.id.next);
    next02.setOnClickListener(advance01);
    back02 = (Button) findViewById(R.id.back);
    back02.setOnClickListener(retreat01);
    output01.setText("Test");
    ImageView image01 = (ImageView) findViewById(R.id.picture);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 3;

    String myJPGPath = Environment.getExternalStorageDirectory() + "/Pictures/00.JPG";
    Bitmap d = BitmapFactory.decodeFile(myJPGPath);
    image01.setImageBitmap(d);
    output01.setText(myJPGPath);

//        String myJpgPath = Environment.getExternalStorageDirectory().toString() + "/0.jpg";
//        String myJpgPath = "/storage/emulated/0/Pictures/0.jpg";
//        File myfile = new File(myJpgPath);
//        output01.setText(myJpgPath);
//        BitmapDrawable d = new BitmapDrawable(getResources(), myJpgPath);
//        image01.setImageDrawable(d);
//        Bitmap d = BitmapFactory.decodeFile(myfile.getAbsolutePath());
//        Bitmap d = BitmapFactory.decodeFile("/storage/emulated/0/0.jpg");
//        image01.setImageBitmap(d);
}
public OnClickListener advance01 = new OnClickListener() {
    public void onClick (View v) {
        test++;
        if (test == 1) {
            output01.setText(text1);

糟糕 - 这里是logcat - 看起来我有权限问题,但图片没有设置为只读。还有一些其他问题 - 也许我会从头开始:

01-03 10:16:55.892  16614-16614/com.test.test I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
01-03 10:16:55.892  16614-16614/com.test.test W/dalvikvm﹕ VFY: unable to resolve virtual method 11346: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
01-03 10:16:55.892  16614-16614/com.test.test D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-03 10:16:55.893  16614-16614/com.test.test I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
01-03 10:16:55.893  16614-16614/com.test.test W/dalvikvm﹕ VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
01-03 10:16:55.893  16614-16614/com.test.test D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-03 10:16:55.894  16614-16614/com.test.test I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
01-03 10:16:55.894  16614-16614/com.test.test W/dalvikvm﹕ VFY: unable to resolve virtual method 9040: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
01-03 10:16:55.894  16614-16614/com.test.test D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
01-03 10:16:55.899  16614-16614/com.test.test I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
01-03 10:16:55.899  16614-16614/com.test.test W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
01-03 10:16:55.900  16614-16614/com.test.test D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-03 10:16:55.900  16614-16614/com.test.test I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
01-03 10:16:55.900  16614-16614/com.test.test W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
01-03 10:16:55.900  16614-16614/com.test.test D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-03 10:16:55.934  16614-16614/com.test.test E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/0.jpg: open failed: EACCES (Permission denied)
01-03 10:16:56.015  16614-16614/com.test.test I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.04.007.066_msm8960_KK_2.7_RB1__release_AU ()
OpenGL ES Shader Compiler Version: 17.01.12.SPL
Build Date: 09/11/14 Thu
Local Branch: mybranch4451390
Remote Branch: quic/kk_2.7_rb1.43
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.04.007.066 + 73685fa + 87c0666 + c4bc670 + 6154422 + 594ea9d +  NOTHING
01-03 10:16:56.062  16614-16614/com.test.test D/OpenGLRenderer﹕ Enabling debug mode 0

2 个答案:

答案 0 :(得分:0)

使用此代码在Pictures文件夹中获取JPG的路径:

String myJPGPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()+"/0.jpg";

此外,除某些SD卡外,Android文件系统区分大小写。所以&#34; 0.JPG&#34;和&#34; 0.jpg&#34;是相同。确保您完全匹配文件名。

答案 1 :(得分:0)

您应该使用Environment.getExternalStorageDirectory().getAbsolutePath()来获取SD卡的完整路径。

试试这个:

 String myJPGPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/00.JPG";