【问题标题】:Get and display image in android from inputstream从输入流获取并在android中显示图像
【发布时间】:2019-01-12 10:58:25
【问题描述】:

首先说我是 Android 语言的新手,我需要一些帮助。

我正在尝试从连接到我的 java 程序的输入流中获取图像并将其保存到内部存储中,然后显示它。但是,我根本没有从我的代码中收到任何错误,但图像根本没有显示。我的 Java 程序/文件没有问题,因为它可以 100% 与我用 Java 编写的另一个程序一起工作,该程序与我尝试对我的 Android 应用程序做的事情相同。

public void GetImage()
{

    try
    {
        InputStream inputStream = new BufferedInputStream(connectionSocket.getInputStream());
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        FileOutputStream out = new FileOutputStream(getFilesDir() + "james.png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    }
    catch (IOException e)
    {
        Log.d("ERROR", "GetImage: " + e);
    }
}
public void DisplayImage()
{
    ImageView myImageview = (ImageView) findViewById(R.id.myImageView);
    int imageResource = getResources().getIdentifier(getFilesDir() + "james.png", null, this.getPackageName());
    myImageview.setImageResource(imageResource);
}

谁能看一下代码并告诉我我做错了什么?谢谢

【问题讨论】:

  • int imageResource的值是多少??

标签: android image bitmap inputstream


【解决方案1】:
    File imgFile = new  File(getFilesDir() + "james.png");

if(imgFile.exists()){
   Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
   ImageView myImageview = (ImageView) findViewById(R.id.myImageView);
   myImageview.setImageBitmap(myBitmap);
}

请用它替换你的方法代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2019-03-13
    • 2013-01-09
    • 2017-03-27
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多