为什么无法显示SD卡中的图像?

时间:2018-10-19 12:27:47

标签: android image storage

我使用此代码显示了SD卡中的图像。结果表明该路径存在并且是正确的,但是没有图像出现。 我该如何解决这个问题?

public class MainActivity extends Activity {

    ImageView processedImgA;
    static String imgSouce = Environment.getExternalStorageDirectory()+ "/folder/image.jpg";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        processedImgA = (ImageView)findViewById(R.id.processedimg_a);

        Bitmap bitmap_Source = BitmapFactory.decodeFile(imgSouce);

        processedImgA.setImageBitmap(bitmap_Source);


    }



}

2 个答案:

答案 0 :(得分:0)

尝试

String imgSouce = Environment.getExternalStorageDirectory()+ "/folder/image.jpg";
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imgSouce, bmOptions);
processedImgA.setImageBitmap(bitmap);

还请确保您已添加READ_EXTERNAL_STORAGE权限,并检查了6.0版设备的运行时

答案 1 :(得分:0)

如果您应用的targetAPI为23+(Android 6.0+),则应先请求读取权限,然后再从存储SD卡读取文件。 我的建议是您可以使用rxpermission请求权限。

$routerAddress = "192.168.10.126"
$port = "23"
$tcp = New-Object System.Net.Sockets.TcpClient($routerAddress,$Port)
$tcpstream = $tcp.GetStream()
$reader = New-Object System.IO.StreamReader($tcpStream)
$writer = New-Object System.IO.StreamWriter($tcpStream)
$writer.AutoFlush = $true

while ($tcp.Connected)
{       
write-host ([char]$reader.Read()) -NoNewline
while(($reader.Peek() -ne -1) -or ($tcp.Available)){        
    write-host ([char]$reader.Read()) -NoNewline
}
if ($tcp.Connected)
{
    Write-Host -NoNewline "_"
    $command = Read-Host

    if ($command -eq "escape")
    {
        break
    }
    $writer.WriteLine($command) | Out-Null
}     
}
$reader.Close()
$writer.Close()
$tcp.Close()

///您应该在app / build.gradle文件中的上方添加两行代码。

implementation com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar//for add rxpermissions
implementation io.reactivex.rxjava2:rxjava:2.1.4//for add rxjava