【问题标题】:Dynamically load jpg files动态加载jpg文件
【发布时间】:2014-10-11 00:25:22
【问题描述】:

我想根据我按顺序读取的 txt 文件在图像视图中加载不同的 jpg 文件。我已将 jpg 文件放在资产中。 我发现以下代码可以使用:

((ImageView)view).setImageBitmap(BitmapFactory.decodeFile("/data/data/com.myapp/files/someimage.jpg"));

我尝试插入:

(GetApplicationContext().getResources().getAssets()+"file.jpg")

知道我用android studio有什么推荐:

  1. 如何找到我的“资产”路径

  2. 我应该使用 assets 目录吗

  3. 关于其他方法的任何建议

目录系统在android上一点也不简单!

【问题讨论】:

    标签: android bitmap drawable


    【解决方案1】:

    如果你想在 assets 目录中加载图片,你可以这样做,

    InputStream is = context.getAssets().open(imageFileName);
    Bitmap bitmap = BitmapFactory.decodeStream(is);

    将图像放入可绘制目录中,然后将它们用作资源。

    int drawableId = context.getResources().getIdentifier("fileNameWithoutExtension", "drawable", context.getPackageName();
    Drawable drawable = context.getResource().getDrawable(drawableId);

    【讨论】:

    • 感谢您的帮助!但两种方法都不起作用。通过调试器一切看起来都很好。除了 thr res 寻找drawable-hdpi-v4。所以我手动创建了这个目录,但它没有改变任何东西。我应该寻找什么?我更改为您的代码的唯一修改是初始化 context = getApplicationContext()。
    【解决方案2】:

    再次感谢您的帮助!非常感谢!

    代码:

    public class MyActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               drawJPG();
            }
        });
    
    }
    
    private void drawJPG() {
        Context context = null;
        InputStream is = null;
        context = getApplicationContext();
        try {
            is = context.getAssets().open("myjpg.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(is);
    }
    

    在 bitmap() 上进行调试看起来不错

    this = {android.graphics.Bitmap@830031440008}
    nativeBitmap = 2052352744
    buffer = {byte[201300]@830031238688}
    width = 275
    height = 183
    density = -1
    isMutable = false
    isPremultiplied = true
    ninePatchChunk = null
    layoutBounds = null
    

    选项 2:

    public class MyActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               drawJPG();
            }
        });
    
    }
    
    private void drawJPG() {
        Context context = null;
        context = getApplicationContext();
        int drawableID = context.getResources().getIdentifier("myjpg", "drawable", context.getPackageName());
        Drawable drawable = context.getResources().getDrawable(drawableID);
    
    }
    

    通过调试看起来还可以:

    this = {android.content.res.Resources@830030928528}
    value = {android.util.TypedValue@830030928808}"TypedValue{t=0x3/d=0x3 "res/drawable-hdpi-v4/myjpg.jpg" a=4 r=0x7f020000}"
    id = 2130837504
    isColorDrawable = false
    key = 17179869187
    cs = null
    dr = null
    file = {java.lang.String@830031232520}"res/drawable-hdpi-v4/myjpg.jpg"
    

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 2012-05-06
      • 2010-09-23
      • 1970-01-01
      • 2011-05-23
      • 2014-11-28
      相关资源
      最近更新 更多